×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: PHP
Posted by: Roman Ignatov
Added: Nov 22, 2016 4:39 PM
Views: 19
Tags: phalcon
  1. <?php
  2.  
  3. use Phalcon\Db\Profiler as ProfilerDb;
  4. use Phalcon\Events\Manager as EventsManager;
  5. use Phalcon\Db\Adapter\Pdo\Mysql as MysqlPdo;
  6.  
  7. $di->set('profiler', function () {
  8.     return new ProfilerDb();
  9. }, true);
  10.  
  11. $di->set('db', function () use ($di) {
  12.  
  13.     $eventsManager = new EventsManager();
  14.  
  15.     // Get a shared instance of the DbProfiler
  16.     $profiler      = $di->getProfiler();
  17.  
  18.     // Listen all the database events
  19.     $eventsManager->attach('db', function ($event, $connection) use ($profiler) {
  20.         if ($event->getType() == 'beforeQuery') {
  21.             $profiler->startProfile($connection->getSQLStatement());
  22.         }
  23.  
  24.         if ($event->getType() == 'afterQuery') {
  25.             $profiler->stopProfile();
  26.         }
  27.     });
  28.  
  29.     $connection = new MysqlPdo(
  30.         array(
  31.             "host"     => "localhost",
  32.             "username" => "root",
  33.             "password" => "secret",
  34.             "dbname"   => "invo"
  35.         )
  36.     );
  37.  
  38.     // Assign the eventsManager to the db adapter instance
  39.     $connection->setEventsManager($eventsManager);
  40.  
  41.     return $connection;
  42. });