zoukankan      html  css  js  c++  java
  • phalcon: plugin 结合Manager事件管理、dispatcher调度控制器 监听sql日志记录或其他拦截出来

    可能用到的类 

    phalconmvcuseplugin

    PhalconMvcDispatcher as MvcDispatcher

    PhalconEventsManager as EventsManager

    代码部分

    public/index.php:

    $di['db'] = function() use($di){
            //log
    
           //事件管理
           $eventManager = new PhalconEventsManager();
    
            //继承了plugins的数据库sql操作类
            $eventQSql = new Mydatabase();
    
            //附上一个侦听者
            $eventManager->attach("db", $eventQSql);
    
            $db=new DbAdapter(array(
                "host"     => "localhost",
                "username" => "root",
                "password" => "",
                "dbname"   => "demo",
                "charset"  => "utf8"
            ));
    
            //绑定事件
            $db->setEventsManager($eventManager);
            return $db;
            
    };
    

      

    app/plugins/Mydatabase.php

    use PhalconDbProfiler,
        PhalconLogger,
        PhalconLoggerAdapterFile as LoggerFile;
    
    class Mydatabase extends PhalconMvcUserPlugin {
    
        protected $_logger;
        protected $_profiler;
    
        public function __construct()
        {
            $this->_profiler = $this->profiler;// new  PhalconDbProfiler();
            $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();
        }
    
    
    
    
    }
    

      每次执行sql语句的时候,发送的sql都会被记录到log文件里面。

  • 相关阅读:
    lodash chunk
    lodash.slice
    ⚡ vue3 全家桶体验
    构建一个简约博皮的过程
    [译] 制作 Vue 3 的过程
    ⚠ | 不要再使用 markdown 主题了!
    win 常用命令
    2020年了,别再重复学习原型了
    删除 linux 导致原来的 win10 进不去
    手写一个文章目录插件
  • 原文地址:https://www.cnblogs.com/achengmu/p/6003393.html
Copyright © 2011-2022 走看看