There is a \phalcon\db\profiler class in Phalcon that can be used to log SQL statements and calculate the time consumed. So how do you use it?
In fact, the manual has provided a method, summarized as follows:
1. Register the profiler service with $di
$di->set (' Profiler ', function () { return New\phalcon\db\profiler ();}, True);
2. Registering the DB service, by the way, registers the event
$di->set (' db ', function () use ($DI) { //Create a new event manager $eventsManager = new \phalcon\events\manager (); Get a shared profiler instance from di $profiler = $di->getprofiler (); Listen for all DB events $eventsManager->attach (' db ', function ($event, $connection) use ($profiler) { //A statement to query for previous events, Profiler begins to record the SQL statement if ($event->gettype () = = ' BeforeQuery ') { $profiler->startprofile ($connection Getsqlstatement ()); } A statement query ends, the record is closed, the results are saved in the profiler object if ($event->gettype () = = ' Afterquery ') { $profiler Stopprofile (); } }); $connection = new \phalcon\db\adapter\pdo\mysql (Array ( "host" = "localhost", "username" and "root", "Password" = "secret", "dbname" = "Invo" ); Binds the event manager to a DB instance $connection->seteventsmanager ($eventsManager); return $connection;});
3. Recall SQL records in the program
Execute some queries robots::find (); Robots::find (Array ("Order" = "name")); Robots::find (Array ("limit" = 30)); Get all Prifler record results, this is an array, each record corresponds to an SQL statement $profiles = $this->di->get (' Profiler ')->getprofiles ();// Traverse output foreach ($profiles as $profile) { echo "SQL statement:", $profile->getsqlstatement (), "\ n"; echo "Start time:", $profile->getinitialtime (), "\ n"; echo "End time:", $profile->getfinaltime (), "\ n"; echo "Consumption time:", $profile->gettotalelapsedseconds (), "\ n";} Get the last SQL statement directly echo $this->di->get (' Profiler ')->getlastprofile ()->getsqlstatement ();
PHALCON: Tracking SQL statements