开发者

How to see MySQL statements and error (if any) in CakePHP shell

开发者 https://www.devze.com 2023-01-24 09:27 出处:网络
I am using CakePHP 1.3 and writing custom shells to run mundane tasks in cronjobs. I am seeing failed Model->save() from time to time but I don\'t know anyway to find out what the exact problem is.

I am using CakePHP 1.3 and writing custom shells to run mundane tasks in cronjobs. I am seeing failed Model->save() from time to time but I don't know anyway to find out what the exact problem is.

Is there a way to display the actual 开发者_如何学GoSQL statements executed and warning/error returned by MySQL in a CakePHP shell?

Thanks.


You can use the following SQL dump task for shells.

http://bakery.cakephp.org/articles/carcus88/2011/04/08/sql_dump_task_for_shells


One way to do this would be to watch the MySQL log file in a separate terminal.

A couple ways of doing this are listed here:

MySQL Query Logging in CakePHP


I found a way to do it. In your shell, add:

function initialize()
{
    Configure::write('debug', 2);
    $this->_loadDbConfig();
    $this->_loadModels();
}

Then whenever you like to see the log, call this function:

    function dump_sql()
    {
        $sql_dump = '';

    if (!class_exists('ConnectionManager') || Configure::read('debug') < 2)
        return false;

    $noLogs = !isset($logs);
    if ($noLogs)
    {
        $sources = ConnectionManager::sourceList();

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

    if ($noLogs || isset($_forced_from_dbo_))
    {
        foreach ($logs as $source => $logInfo)
        {
            $text = $logInfo['count'] > 1 ? 'queries' : 'query';
            $sql_dump .= "cakeSqlLog_" . preg_replace('/[^A-Za-z0-9_]/', '_', uniqid(time(), true));
            $sql_dump .= '('.$source.') '. $logInfo['count'] .' '.$text. ' took '.$logInfo['time'].' ms';
            $sql_dump .= 'Nr Query Error Affected Num. rows Took (ms)';

            foreach ($logInfo['log'] as $k => $i)
            {
                $sql_dump .= $i['query'];
            }
        }
    }
    else
    {
        $sql_dump .= 'Encountered unexpected $logs cannot generate SQL log';
    }
 }


One other approach would be to have all your custom queries in the models/behaviors, and just calling the data/updates from shells. This would give you an extra benefit of being able to reuse those custom SQL in other parts of the project. For example, in unit tests.


In CakePHP 1.2, I was able to get the SQL queries to show up in my console output by adding a Configure::write('debug', 2); call to the bottom of the __bootstrap method in the cake/console/cake.php file.

No need to mess around with specifically calling a dump_sql function like some of these answers, I just automatically get the normal queries like at the bottom of a web page.

0

精彩评论

暂无评论...
验证码 换一张
取 消