I hit upon a slight elaboration of the idea of adding debug( ) calls which I have found very useful for untangling a problematic find( ) or other query. Basically, I added to my controller another action whose only purpose is to display the output of such queries. For example, here is what this action currently looks like (slightly sanitized):
function zztest() { # set up query $user_id = 43; $subject_id = 2; $myaction_data = $this->Myaction->find('all', array( 'conditions' => array( 'Myaction.user_id'=>$user_id, 'Myaction.subject_id'=>$subject_id), 'fields' => array( 'max(fact_count)'))); # output results $this->set('result', $myaction_data); } and here is the corresponding view: zztest.ctp: <?php echo "<b>zztest</b><br>\n"; debug($result); ?> Whenever I have a find( ) or a query( ) or whatever whose behavior is causing problems, I copy and paste it over the lines between the two comments in zztest. Sometimes, I have to set up dummy values for variables, as in this example. Then I run this action in a different browser window: http://mydomain.com/myaction/zztest This will show the output of the query in the handy debug-style format, without messing up the appearance of the real page. Using it, I can immediately check out the query, and even play with it without having to touch my real code. When I have figured out the problem (e.g., what combination of numbers and strings I actually get back as keys for my data), then I can fix my real code and move on. What's more, while I'm figuring this out, I don't have to clutter up my real code with bits of debugging. Also, it can be pretty handy to have your app running in one browser window and zztest in another. That way, you are not having to flip back and forth between two different URLs in the same browser window, but just flip between the two windows if you need to. Of course, you can use this same idea of a special purpose debug action for any kind of experimentation, such as trying out different library functions, or just learning how PHP works. On Mar 20, 11:48 am, brian <bally.z...@gmail.com> wrote: > You might want to consider using the Model's afterFind() method, > instead. But, if you'd rather do it in the controller, the best way to > figure out how to access your data is to toss a debug($mydata) in > there. (seems to be my standard response these days ;-) You're > probably getting hung up on the numerical indexes for a hasMany > relation or something. > > On Fri, Mar 20, 2009 at 11:19 AM, ryanam1 <ryan...@gmail.com> wrote: > > > Hi, > > > How can I access data within a controller? > > > Lets say I have the following find() method in my controller > > > $mydata=$this->User->find('all', array('conditions'=>'new;), > > 'fields'=>array('name')) > > > Let's say I wanted to do some sort of proccessing to that data before > > outputting to a view.... How would I do that? > > > I see examples of how to output that data in the view but I see none > > about accessing the data from within the controller. > > > I figure I should do a loop but how should I write $mydata? > > > Should it be like this: $mydata['User']['name'] ? That seems to > > work in the view but I get a "Undefined Index" error when I write it > > that way from within the controller. > > > I don't have the issue when a method returns one record such as field > > () because it is not returning an array.... > > > I hope I'm not confusing anyone :( --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~----------~----~----~----~------~----~------~--~---