[CakePHP]How to check SQL queries in Shell and Controller

[CakePHP1.3.3]

When you debug, within Controller and Model there is often something like SQL from the instant you’ve done $this->find().

At that time, either put the function below into vendor or if you read it in bootstrap you can confirm it in SQL any time.

Incidentally, within Shell things stripped of HTML are output so it’s very convenient.

When you set Configure::write(‘debug’, 2) or higher, it works.

function sql_dump($dbConfig = 'default', $exit = true) {
    ConnectionManager::getDataSource($dbConfig)->showLog();	
    if ($exit) { exit; } 
}

How to use it is as seen below.

class SampleShell extends Shell {
    public $uses = array('Sample');
    
    public function main() {
        $this->Sample->find('all');
        sql_dump();
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.