We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

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


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...

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') {
$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.