zoukankan      html  css  js  c++  java
  • thinkphp5 如何使用查询事件?

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

    就像是注册事件和前置方法或后置方法类似

    下面是demo

    <?php
    namespace appindexcontroller;
    
    use thinkdbQuery;
    use thinkDb;
    use thinkController;
    
    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对象

  • 相关阅读:
    12.12
    12.11
    1208
    1206
    2018-12-23丛晓强作业
    2018-12-17面向对象总结
    2018-12-17-丛晓强作业
    2018-12-13丛晓强作业
    2018-12-12丛晓强作业
    2018-12-11丛晓强作业
  • 原文地址:https://www.cnblogs.com/niuben/p/10275404.html
Copyright © 2011-2022 走看看