Noob Question: Clean Way to View Queries

I have xdebug installed and when I do a var_dump on my queries it formats OK but 90% of it is not what I am looking for (schema, object info etc.). Is there a simple method to simply view the results I want nicely formatted (the target table rows and related table rows). CakePHP has a nice debug() function that does just this, and I am looking for the same sort of thing in Yii.

So you want to see or profile the actual SQL queries?

You should enable CProfileLogRoute of your log-component + enableProfiling of your db-component like this:

config/main.php:




'db' => array(

  // your db connection goes here, just enable profiling in addition

  'enableProfiling' => true,

),

'log' => array(

  'routes' => array(

    array(

      'class' => 'CProfileLogRoute',

      'report' => 'summary', // execution time summary, slowest on top (default)

      //'report' => 'callstack', // reflecting the calling sequence

    ),

  ),

),



Hope that helps :)

Thank you. I see that shows my queries, but what I want to see is the query results, especially relational ones, nicely formatted. Is there a Yii method to show that?

I am new to Yii and I am assuming that Yii returns related field data in a single query. That seems to be the case from Yii’s documentation. It is just not obvious in the results that are var dumped. If Yii does not return related field data, I suppose that I could run getRelated on a query result, but I imagine that would slow things down quite a bit as I iterated through a long query.

The AR method returns results in an array of AR objects. If you use Yii::trace() you will see a verbose output of the results. You might also find the Yii Debug Toolbar of some use.

I am using the Yii Debug Toolbar, and it is nice for environmental parameters, but it does not show query results. I tried increasing my trace level to 5 in the index.php file, but that seemed to have no effect on showing a relational return, clear or verbose. Apparently query returns in Yii are opaque and I can just keep using getRelated() as I iterate through a query. It is just a different way of looking at query returns than in Cake, which shows a relational return as a simple array.

That’s probably because the Yii AR is a heck of a lot more powerful than the Cake model. Remember that you are not getting a simple array back in many cases, often you are getting an array of objects, which can contain other objects and so on. Representing that in a table is not going to work.

If there is a particular result I want, I’ll often use the Yii::trace() method to view it. Certainly, it’s not a pretty table, however it is often simple to do something like:




foreach ($results as $key => $object){

  Yii::trace("Result $key: ".print_r($object->getAttributes(),true));

}



as this will return just the attributes (generally translating to columns) of the object(s). Combine this with Firebug (or the built in developer tools console in Chrome) and you’ll get an easily understood read-out of each of the results. An example is:




[15:11:46.730][trace][application]Result 0: Array

(

    [id] => 312

    [user_id] => 95

    [w_text] => I can't find where to comment on a winjje on my itouch


    [timestamp] => 2010-05-27 09:45:59

    [logged_ip] => xxx.xxx.xxx.xxx

    [postcode] => 

    [latitude] => 

    [longitude] => 

    [client] => Winjje for iPhone v1.0.0publicbeta1

    [reply_to] => 

    [edit_time] => 0000-00-00 00:00:00

    [edited] => 

    [is_edit] => 0

    [deleted] => 0

)