Cakephp在Controller中显示sql语句
Cakephp在Controller中查询语句一般是:
$this->Model->find();
那么这条语句对应的sql语句是什么呢?
可以通过下面方法显示:
1.
$dbo = ConnectionManager::getDataSource('default');
$logs = $dbo->getLog();
$lastLog = end($logs['log']);
echo $lastLog['query'];
2.
$dbo = $this->Model->getDatasource();
$logData = $dbo->getLog();
$getLog = end($logData['log']);
$this->log( $getLog['query']);
3.
/** * 获取SQL执行的日志
* return array */
function printSQL() {
$sources = ConnectionManager::sourceList(); $logs = array();
foreach ($sources as $source) {
$db =& ConnectionManager::getDataSource($source); if (!$db->isInterfaceSupported('getLog')) continue; $logs[$source] = $db->getLog(); }
return $logs;
}