<?php
use Phalcon\Events\Event,
Phalcon\Db\Adapter;
class DbEventListener extends \Phalcon\Mvc\User\Component {
public function beforeQuery(Event $event, Adapter $connection) {
$this->dbProfiler->startProfile($connection->getSQLStatement(), $connection->getSQLVariables(), $connection->getSQLBindTypes());
}
public function afterQuery(Event $event, Adapter $connection) {
$this->dbProfiler->stopProfile();
}
}
<?php
# Attach EM to database adapter
$di['db']->setEventsManager($di['eventsManager']);
# Create a database profiler
$di['dbProfiler'] = new \Phalcon\Db\Profiler;
# Attach a listener to log each DB query
$di['eventsManager']->attach('db', new DbEventListener);
# Get total statements
$nQueries = $di['dbProfiler']->getNumberTotalStatements();
You can also get the queries as raw SQL, bind types etc