thinkphp5 如何使用查询事件?

它是对数据库的CURD操做事件进行了钩子,当事件触发时,会进行回调。php

就像是注册事件和前置方法或后置方法相似数据库

下面是demo数组

<?php namespace app\index\controller; use think\db\Query; use think\Db; use think\Controller; class Index extends Controller { public function _initialize() {      //parent::_initialize() 是调用父类的_initialize方法,若是你的父类_initialize函数没有任何内容,不须要写parent::_initialize()
        parent::_initialize(); //注册一个select查询事件
        Query::event('before_select',function($option,$query){ echo 'this is query event'; }); } public function index() { $blog = Db::table('blog')->select(); print_r($blog); } }

 会看到在使用查询的同时执行了查询事件,打印出了 'this is query event ' ,而且先于print_r($user);
其中:回调函数中有两个参数:$options, $query
前一个表示关于当前查询的一个数组信息,后一个就是Query对象app

相关文章
相关标签/搜索