phalcon: Profiling分析 profilter / Plugin結合,dispatcher調度控制器 監聽sql執行日誌

我的以爲profilter 跟 logger 功能差很少,logger的功能在於寫入,profilter功能在於sql後及時顯示分析。都是對sql執行的的分析:一個是寫入log文件,一個是直接在頁面展現。php

下面看例子,sql

public/index.php:app

$di->set('profiler', function(){
        return new \Phalcon\Db\Profiler();
    }, true);

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

        //profile
        $eventManager = new \Phalcon\Events\Manager();
        $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;

};

  

 

app\plugins\ProfilerEManger.phpthis

use \Phalcon\Mvc\User\Plugin;


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 \Phalcon\Mvc\Controller {

    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);
    }

    

}
相關文章
相關標籤/搜索