Doctrine 1.x log all queries

To log all queries we need to use Event Listeners

class QueryDebuggerListener extends Doctrine_EventListener
{
    public function preStmtExecute(Doctrine_Event $event)
    {
        $q = $event->getQuery();
        $params = $event->getParams();

        while (sizeof($params) > 0) {
            $param = array_shift($params); 

            if (!is_numeric($param)) {
                $param = sprintf("'%s'", $param);
            }   

            $q = substr_replace($q, $param, strpos($q, '?'), 1); 
        }   
        error_log($q);
    }
}
$queryDbg = new QueryDebuggerListener();
$dbh = new PDO($dsn, $user, $password);
$conn = Doctrine_Manager::connection($dbh);
$conn->addListener($queryDbg);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s