Classes that are likely to be used
Phalcon\mvc\use\plugin
Phalcon\mvc\dispatcher as Mvcdispatcher
Phalcon\events\manager as Eventsmanager
Code section
public/index.php:
$di [' db '] = function () use ($di) { //log //Event Management $eventManager = new \phalcon\events\manager (); Database SQL Operation class inherited from plugins $eventQSql = new \mydatabase (); Attach a listener $eventManager->attach ("db", $EVENTQSQL); $db =new Dbadapter ( "host" + "localhost", "username" and "root", " password" and "=", "dbname" = "Demo", "CharSet" and " UTF8" ); Bind event $db->seteventsmanager ($eventManager); return $db; };
app/plugins/mydatabase.php
Use Phalcon\db\profiler, Phalcon\logger, phalcon\logger\adapter\file as Loggerfile;class Mydatabase extends \phalcon\mvc\user\plugin { protected $_logger; protected $_profiler; Public function __construct () { $this->_profiler = $this->profiler;//new \phalcon\db\profiler (); $this->_logger = new Loggerfile (".. /log/sql.log "); } Public Function BeforeQuery () { $this->_logger->log ($this->db->getsqlstatement (), Logger::info); $this->_profiler->startprofile ($this->db->getsqlstatement ()); } Public Function Afterquery () { $this->_logger->log ($this->db->getsqlvariables (), Logger::info); $this->_profiler->stopprofile (); }}
Each time the SQL statement is executed, the SQL sent will be logged into the log file.
Phalcon:plugin combined with manager event management, dispatcher scheduling controller monitors SQL logging or other interception