• Scope 功能的改进


    前段时间发表了一篇文章 面向对象的一小步:添加 ActiveRecord 的 Scope 功能 提到一种更加友好的方式做数据库查询。经小伙伴的建议,在满足同样条件下,可以有更为简洁的封装方法。

    这需要用到重载ActiveQuery方法,在通过gii建数据模型model时,可以勾选"Generate ActiveQuery"这么一项自动生成。

    例如,在生成Student Model时,可以自动生成StudentQuery

    class StudentsQuery extends yiidbActiveQuery
    {
        /**
         * {@inheritdoc}
         *
         * @return TbStudents[]|array
         */
        public function all($db = null)
        {
            return parent::all($db);
        }
    
        /**
         * {@inheritdoc}
         *
         * @return TbStudents|array|null
         */
        public function one($db = null)
        {
            return parent::one($db);
        }
    
        //默认筛选已经审核通过的记录,当然,也可以自己定义$status
        public function checked($status = 1)
        {
            return $this->where(['check_status' => $status]);
        }
    }
    

    checked()方法里面有一条where条件,也可以添加多条。总之,这个checked方法可以随你定义, 封装一类现实应用常用的条件组合。

    这样,我们便可以这样来查询:

    Student::find()->checked()->where(...)->all();
    

    那又如何满足checked方法的静态调用呢?

    Student::checked()->where(...)->all();
    

    这还得在Model里面重载__callStatic()方法。这个重载可写在一个如BaseModel的公共方法里面,以便大家调用。

    public static function __callStatic($name, $arguments)
    {
    	return static::find()->$name(...$arguments);
    }
    

    也更为简洁,同时也是一种透明操作。在StudentQuery中没有定义对应方法或者传参错误都会导致报错。

    那么这是如何做到的?
    因为在Student里面有这么一段:

    public static function find()
    {
    	return new StudentQuery(get_called_class());
    }
    

    可见在__callStatic中返回的static::find()其实就是一个StudentQuery的一个实例,然后在这个实例中去寻找checked方法。绕了个小圈子,重新回到了StudentQuery。道理非常简单。

    现在我们同样可以实现两种友好的查询了:

    Student::find()->checked()->where(...)->all();
    Student::checked(2)->where(...)->all()
    

    对代码简洁性和透明性的要求比较高的小伙伴,可以采用这种封装。

  • 相关阅读:
    视频直播架构
    day1 python 入门
    python 多用户登录
    mysql innobackup 备份脚本
    ADT离线安装
    真机调试adb:wait for device 解决方案
    php中的魔术方法
    整理资料
    PostgreSQL表空间_数据库_模式_表_用户角色之间的关系[转]
    PHP获取文件夹的大小
  • 原文地址:https://www.cnblogs.com/minirice/p/9399697.html
Copyright © 2020-2023  润新知