Last Updated: February 25, 2016
·
1.325K
· raphaelstolt

Log Propel queries in a Silex application

To add the logging of Propel (v1.6.9) generated SQL queries to a Silex application, Silex middlewares once again come in quite handy. The following snippet assumes the application uses the builtin MonologServiceProvider for logging.

Before Middleware to enable debugging

$app->before(function (Request $request) use($app) {
    if ($app['debug']) {
        $con = \Propel::getConnection();
        $con->useDebug(true);
    }
});

After Middleware to do the actual logging

It also logs the called route which triggered the query or queries.

$app->after(function (Request $request) use($app) {
    if ($app['debug']) {
        $con = \Propel::getConnection();
        $app['monolog']->addDebug(
            'propel-query', 
            array('route' => $request->getPathInfo(),
                  'sql' => $con->getLastExecutedQuery())
        );
    }
});

An exemplary log entry

[2013-07-12 14:13:12] example-app.DEBUG: propel-query {"route":"/navigation/page-assignment","sql":"SELECT page_pool.autor, page_pool.id, page_pool.node_id, page_pool.metafile, page_pool.rubriken_id, page_pool.template, page_pool.timestamp, page_pool.url, page_pool.title FROM `page_pool` ORDER BY page_pool.title ASC LIMIT 10"} []

Doh!

This solution only logs the last query triggered by a route; and therefore might not be that handy for batches of queries.