how to print query in CakePHP

3.9k views Asked by At

Actually,

I have written this code in AppController.php in CakePHP 2.2.2

class AppModel extends Model
{
  function getLastQuery()
  {
      $dbo = $this->getDatasource();
      $logs = $dbo->getLog();
      $lastLog = end($logs['log']);
      return $lastLog['query'];
  }
}

I tried following code to print output query

echo $this->AssetModel->getLastQuery(); 
$this->render('sql');

Is any body know to the point solution for that....?

2

There are 2 answers

0
Harendra Singh On

Hi you can use the below line to put into your layout file

<?php  echo $this->element('sql_dump'); ?>

this will print all running query running in current action.

You can also use cakephp debugkit plugin.

0
Jacek B Budzynski On

I use this in CakePHP 2.6.11 to print Queries from Model in browser

debug($this->YOURMODEL->getDataSource()->getLog(false, false)); exit;