1

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 SQL statements executed and warning/error returned by MySQL in a CakePHP shell?

Thanks.

5 Answers 5

2

You can use the following SQL dump task for shells.

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

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

Comments

0

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

Comments

0

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';
    }
 }

Comments

0

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.

Comments

0

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.

1 Comment

Thanks, 6-months-ago me!

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.