phalcon: Profiling分析 profilter / Plugin结合,dispatcher调度控制器 监听sql执行日志

个人觉得profilter 跟 logger 功能差不多,logger的功能在于写入,profilter功能在于sql后及时显示分析。都是对sql执行的的分析:一个是写入log文件,一个是直接在页面展示。

下面看例子,

public/index.php:

$di->set('profiler', function(){
        return new PhalconDbProfiler();
    }, true);

$di['db'] = function() use($di){       

        //profile
        $eventManager = new PhalconEventsManager();
        $profiler = new ProfilerEManger();
        $eventManager->attach('db', $profiler);
        $db = new DbAdapter(array(
            "host"     => "localhost",
            "username" => "root",
            "password" => "",
            "dbname"   => "demo",
            "charset"  => "utf8"
        ));
        $db->setEventsManager($eventManager);
        return $db;

};

  

apppluginsProfilerEManger.php

use PhalconMvcUserPlugin;


class ProfilerEManger extends Plugin {



    public function beforeQuery()
    {
       // var_dump($this->db->getSqlStatement());exit;
        $this->profiler->startProfile($this->db->getSqlStatement());
    }

    public function afterQuery()
    {
        $this->profiler->stopProfile();
    }





}

  

ProfilerController.php
class ProfilerController extends PhalconMvcController {

    public function indexAction()
    {
        $users = array();
        $use = Users::findFirst("id = 1");
        if($use)
        {
            $users = $use->toArray();
        }
        var_dump($users);

        $profile = $this->profiler->getLastProfile();
        var_dump($profile);
    }

    

}

  



原文地址:https://www.cnblogs.com/achengmu/p/5996933.html