CakePHP SQL debug - Configure the CakePHP debug output setting

CakePHP SQL debug tip: If you're a CakePHP newbie, one of the surprising things about seeing your first CakePHP view pages is seeing the CakePHP debug output (the SQL debug output) at the bottom of your view pages. It's definitely a nice feature for when you're learning CakePHP, and also for any time you're trying to understand what queries CakePHP is running for you, but it's a surprise at first.

This nice CakePHP debug output feature also leads to a CakePHP FAQ: How do I turn off (configure) the CakePHP SQL debug output at the bottom of my view pages?

How to configure the CakePHP SQL debug output

To configure the CakePHP SQL debug output, just open the $app/config/core.php file, and look for the debug parameter. In CakePHP version 1.2.5 the line you're looking for looks something like this:

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

By default, this 'debug' parameter is set to a value of 2, which is one of the CakePHP development mode options.

CakePHP currently supports four debug mode settings, which you control by setting this CakePHP debug parameter to a value of 0-3. Here's how the CakePHP core.php configuration file describes these four levels:

/**
 * CakePHP Debug Level:
 *
 * 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. (NOTE: Not in CakePHP 1.3) 
 *
 * In production mode, flash messages redirect after a time interval.
 * In development mode, you need to click the flash message to continue.
 */

As you can see from those CakePHP SQL debug docs, when your application goes into production mode, you'll want to set this CakePHP debug parameter to 0, like this:

// set the cakephp debug level to production mode
Configure::write('debug', 0);

Note: In CakePHP version 1.2.5, which I'm currently using, this CakePHP debug parameter is set to 2 by default.

CakePHP SQL debug - CakePHP 1.3

As I learned just moments ago, in CakePHP 1.3, if you want to see CakePHP SQL debug output in your CakePHP view pages, you need to add this line to your view page:

echo $this->element('sql_dump');

You can also put this line in your $app/views/layouts/default.ctp file, and in fact, that may be the best thing to do while you're in CakePHP debugging mode.

Sorry, I don't know enough about CakePHP 1.3 yet to know why this line of code isn't built into a CakePHP view page when running "cake bake view" in CakePHP 1.3. (I think in earlier CakePHP versions you needed to add "echo $cakeDebug" to your views, but I'd have to look at some old code to be sure.)