?

Log in

No account? Create an account
entries friends calendar profile Previous Previous Next Next
Logging sql queries into file in CakePHP 2 - Vladimir Bilyov
volod_0x16
volod_0x16
Logging sql queries into file in CakePHP 2
Sometimes you need to see sql queries log, but output on webpage is not handy. So, I've created datasource that logs queries into file. It was simple

So, here's what CakePHP says about custom DataSources. The one we need is just an extention of Mysql datasource, so we'll have only one method.

1. Create new file app/Model/Datasource/Database/MysqlEx.php
    App::uses('Mysql', 'Model/Datasource/Database');
/**
* Logs MySQL queries into debug file
*
* @author Vladimir Bilyov 2012-03-04 19:03:03
* @version 1.0
*/
class MysqlEx extends Mysql {

function logQuery($sql) {
parent::logQuery($sql);
if (Configure::read('Cake.logQuery')) {
debug('sql[' . $this->_queriesCnt] . ':' . $sql);
}
}
}



2. in app/Config/core.php add:
Configure::write('Cake.logQuery', 1);

3. in app/Config/database.php replace:
'datasource' => 'Database/Mysql'
with
'datasource' => 'Database/MysqlEx'

done :)

Tags:

4 comments or Leave a comment
Comments
From: (Anonymous) Date: July 9th, 2013 08:43 pm (UTC) (Link)

deosnt work for me

Thanks, I need this, but I cant get it to work. I assume MysqlEx file should be MysqlEx.php? I get Missing Datasource and see your file (MysqlEx) output to the top of my main web page. any ideas? thanks Joel
volod_0x16 From: volod_0x16 Date: July 11th, 2014 10:26 am (UTC) (Link)

Re: deosnt work for me

sorry, a bit late
fixed file name
From: (Anonymous) Date: July 10th, 2014 12:55 pm (UTC) (Link)

Error

Missing Datasource

Error: Datasource class MysqlEx could not be found.
volod_0x16 From: volod_0x16 Date: July 11th, 2014 10:25 am (UTC) (Link)

Re: Error

there are 2 options here:
1. you didn't create MysqlEx.php file and class in it
2. something has changed in CakePHP since 2012 ;)
4 comments or Leave a comment