A CakePHP debug strategy

I just started working with CakePHP in the last two weeks, and as a newbie, I've found a simple debug strategy that I like. I've used this CakePHP debug technique several times when trying to formulate a CakePHP find query.

Here's a quick example. I was just trying to figure out how to call a CakePHP find method where I to get all the distinct date values from a database table, and also add a SQL "ORDER BY" clause to the same SQL query. As I was trying to get my find function syntax correct, I put the results from the find function call into a variable I named $results, like this:

// show the dates that we have log files for
function distinct() {
  $results = $this->LogfileRecord->find('all',
    array('fields'=>array('DISTINCT date'), 'order'=>array('date DESC'))
  $this->set('results', $results);

As you can see from that code, after storing the results in the $results variable, I think used the CakePHP set function to make these results available to my CakePHP view page.

If you're familiar with CakePHP, you'll know that after my set function call, CakePHP will render the view page that corresponds to my distinct method, and by convention that page should be in the proper folder, and should be named distinct.ctp.

This is where the favorite part of this debug process comes in. While I'm debugging a problem like this, my view page consists of just one line of PHP code:

<? echo debug($results); ?>

To be clear I'll say it again: I don't have anything else in this CakePHP view page; just this debug call.

I like this approach because (1) it's very fast to set up, and (2) all I have to do to run my query is call the distinct page in my web browser, something like this:


Hitting this URL invokes the distinct method in my LogfileRecordsController (which I showed above). That method runs my SQL query using the CakePHP find method, places the results in the $results variable, and then uses the CakePHP controller set method to make the $results contents available to the view, which I then display with the debug function.

The debug parameter in core.php

Of course when you're debugging CakePHP SQL queries, having the CakePHP debug variable set properly is also very helpful. When this variable is set properly in the $app/config/core.php file, it tells CakePHP to display the SQL query the find function is using behind the scenes at the bottom of your CakePHP view page.

By default this debug parameter is set to a value of 2 in this configuration file, like this:

Configure::write('debug', 2);

If you're not familiar with this CakePHP debug parameter, here is a quick bit of documentation, taken directly from the $app/config/core.php file:

* Production Mode:
* 0: No error messages, errors, or warnings shown. Flash messages redirect.
* Development Mode:
* 1: Errors and warnings shown, model caches refreshed, flash messages halted.
* 2: As in 1, but also with full debug messages and SQL output.
* 3: As in 2, but also with full controller dump.

When I'm debugging a CakePHP SQL query (i.e., a CakePHP find method call), I like to do both of these things: (1) Use my debug call in the view page, and (2) set the 'debug' parameter in the $app/config/core.php file to 2 or 3.