How to get last SQL-query text (insert/update) after saving object?

Hello!

Is there way to get last SQL-query text that was sent to database with Phalconphp after \Phalcon\MVC\Model::save()?

Now, $this->modelsManager->getLastQuery() returns null...



24.2k
edited Oct '14

I found solution, but it seems not good.

If I added "event-manager" code before save-object action:

$connection = $this->di->getShared("db");
$eventsManager = new \Phalcon\Events\Manager();
$eventsManager->attach('db', function($event, $connection)
 {
   if ($event->getType() == 'beforeQuery') {
    $connection->getSQLStatement();
  }
});
$connection->setEventsManager($eventsManager);
$object->save();
$sql = $connection->getSQLStatement();

then $sql will be the latest sql-query.

But why I need this "event-manager" code?

I found the easiest way was not in PHP, but by turning on the query log in MySQL, then just watching that log as queries came in.