Register MySQL Query in CakePHP

I wanted to know if there is a way to write mysql queries in CakePHP executed when we use the find method on models, I know that the database queries are rails, so Cake does the same, if so, how can I enable it or use?

Shiv

+4
source share
5 answers

Assuming you are on nix os, a better approach would actually lead to the mysql log itself.
You could learn some interesting things from this.

login to Ubuntu when installing from the repository

tail -f /var/log/mysql/mysql.log 

As mentioned below, this is a huge performance killer (well, all logs have some effect on performance). Therefore, make sure that you only use it on your dev / QA machines and only for a short time on your production machine.

+1
source

This page contains instructions on how to get Cake to register requests in the same way as rails.

+6
source

A Very simple way to log all executed requests:

in cake \ libs \ model \ datasources \ dbo \ dbo_mysql.php

find the _execute function:

 function _execute($sql) { return mysql_query($sql, $this->connection); } 

add the line " $ this-> log ($ sql); " before " return mysql_query ($ sql, $ this-> connection); "

 function _execute($sql) { $this->log($sql); return mysql_query($sql, $this->connection); } 

What is it!!!!! All your SQL queries are logged. Verify that the log file is configured correctly and has sufficient permissions. Enjoy

+4
source

CakePHP 1.3 uses the sql_dump element for this.
You can use the element directly if Configure::read('debug') set to 2:

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

Or take the code directly if you need to do something else with it (e.g. echo it from ShellTask)

 $sources = ConnectionManager::sourceList(); $logs = array(); foreach ($sources as $source): $db =& ConnectionManager::getDataSource($source); if (!$db->isInterfaceSupported('getLog')): continue; endif; $logs[$source] = $db->getLog(); endforeach; 

Echo with:

 print_r($logs) 
+1
source

This is what I use (put it in the items folder, then include in your layout)

 <?php ob_start(); echo $this->element('sql_dump'); $out = ob_get_contents(); ob_end_clean(); CakeLog::write('mysql' , $out); ?> 

then you will find the mysql.log file in TMP.logs.DS.mysql.log

+1
source

All Articles