PHALCON: Tracking SQL statements

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.