4

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

Shiv

5 Answers 5

6

This page has instructions on how to get Cake to log queries the same way as rails.

Sign up to request clarification or add additional context in comments.

Comments

4

A Very simple method to log all the queries being executed:

in your 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);
}

That's it!!!!! All your SQL Queries gets logged. Make sure the log file is configured properly and have sufficient permission. Enjoy

1 Comment

It is bad practice to directly alter core CakePHP code. This method should be avoided. If you want to alter the behaviour of Cake extend it in your app directory.
1

Assuming you are on a nix os, the best approach would actually to tail the mysql log itself.
You might learn some interesting things out of it.

log in Ubuntu when installing from repository

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

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

1 Comment

by default, mysql.log is disabled because it's a huge performance killer, You'll need to disable it from my.cnf and only do so in dev. environment!
1

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

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

Or take it's code directly if you need to do something else with it (like echo it from a 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 e.g.:

print_r($logs)

Comments

1

This is what I use (put it in element 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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.