Full Query Logging
Actually, since Propel 1.3 uses PDO, it is not possible to get the full query that was executed since Propel uses prepared statements and PDO does not replace the placeholders the way Creole does. To help work around this, however, Propel provides a DebugPDO subclass which will provide logging of the statements and log the values that are being substituted into the prepared statement.
Note that you can also get a decent representation of the criteria being used in a SELECT query by using the Criteria->toString() method.
Configure Propel to use DebugPDO
You must tell Propel that you wish to use the DebugPDO class (instead of the default PropelPDO class). This is accomplished in the file runtime-conf.xml (see the runtime configuration referencefor more details).
<?xml version="1.0"?>
<config>
<propel>
<datasources default="bookstore">
<datasource id="bookstore">
<adapter>sqlite</adapter>
<connection>
<!-- the classname that Propel should instantiate, must be PropelPDO subclass -->
<classname>DebugPDO</classname>
A few notes about the class specified here:
- You may use a class packaged with Propel (Propel provides PropelPDO (default) and DebugPDO PDO subclasses) or you may use your own class (more on that later).
- If you use your own class, it needs to already be included -- or you must have an autoloader registered that knows how to find it.
- The class specified must extend PropelPDO. Propel requires certain fixes to PDO API that are provided by the PropelPDO subclass.
Logged Queries
By default the DebugPDO connection will log queries and bind param values using the Propel::log() static method. This will use the log configured by the <log> element in the {{runtime-conf.xml}} file or it will use the logger that is set by the Propel::setLogger() method.
Changing the Log Level
By default the log messages will be logged at the Propel::LOG_DEBUG level. This can be changed by calling a setter method on the connection.
<?php $con = Propel::getConnection(MyObjPeer::DATABASE_NAME); if ($con instanceof DebugPDO) { $con->setLogLevel(Propel::LOG_INFO); }
Now all queries & bind param values will be logged at INFO level.
Configuring a Different Logger
If you would like the queries to be logged using a different logger (e.g. to a different file, or with different ident, etc.), you can set a logger explicitly on the connection:
<?php $con = Propel::getConnection(MyObjPeer::DATABASE_NAME); if ($con instanceof DebugPDO) { $logger = Log::singleton(...); $con->setLogger($logger); }
The same rules that apply to the general logging configuration apply here too. Specifically:
- You can use a PEAR logger or a logger that implements Propel's BasicLogger interface.
Query Counting
The DebugPDO class keeps track of the number of queries that are executed. You can get the number of queries at the end of the request from the connection object:
<?php /* At end of request */ $con = Propel::getConnection(MyObjPeer::DATABASE_NAME); if ($con instanceof DebugPDO) { print "Number of queries executed: " . $con->getQueryCount(); }
- You cannot use persistent connections if you want the query count to work (actually DebugPDO in general requires that you not use persistent connections in order for it to correctly log bound values and count executed statements).
Custom PDO Object
While this document describes the DebugPDO object, you can also specify your own PropelPDO subclass in the runtime-conf.xml. Note the rules, mentioned above, about the class being already included or autoload-able.
