定制 sam-it/yii2-virtual-fields 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

sam-it/yii2-virtual-fields

Composer 安装命令:

composer require sam-it/yii2-virtual-fields

包简介

Implementation of virtual fields for Yii2 AR

README 文档

README

Implementation of virtual fields for Yii2 AR

To work this library needs to change 2 parts of the Yii2 ORM.

  • The model definitions
  • The query implementation

Configuration

The change to ActiveQuery are simple and can be applied using a trait or a behavior. In case you did not subclass ActiveQuery you can choose to attach the behavior dynamically.

use SamIT\Yii2\VirtualFields\VirtualFieldQueryBehavior;
use SamIT\Yii2\VirtualFields\VirtualFieldBehavior;
use yii\db\ActiveRecord;
use yii\db\ActiveQuery;
class Author extends ActiveRecord 
{
    /**
     * Attach the behavior after constructing the query object  
     * @return ActiveQuery
     */
    public static function find()
    {
        $query = parent::find();
        $query->attachBehavior(VirtualFieldQueryBehavior::class, VirtualFieldQueryBehavior::class);
        return $query;
    }
    
    public function getPosts(): ActiveQuery
    {
        return $this->hasMany(Post::class, ['author_id' => 'id']);
    }

    public function behaviors() 
    {
        return [
            VirtualFieldBehavior::class => [
                'class' => VirtualFieldBehavior::class,
                'virtualFields' => [
                    'postCount' => [
                        VirtualFieldBehavior::LAZY => function(Author $author) { return $author->getPosts()->count(); },
                        VirtualFieldBehavior::CAST => VirtualFieldBehavior::CAST_INT,
                        VirtualFieldBehavior::GREEDY => Post::find()
                            ->andWhere('[[author_id]] = [[author]].[[id]]')
                            ->limit(1)
                            ->select('count(*)')
                    ],
                    'postCount2' => [
                        VirtualFieldBehavior::LAZY => function(Author $author) { return $author->getPosts()->count(); },
                        VirtualFieldBehavior::CAST => VirtualFieldBehavior::CAST_INT,
                        // Sometimes you might want to defer loading of your greedy definition in such cases you may supply a closure.
                        // This closure will be called only once
                        VirtualFieldBehavior::GREEDY => static fn() => Post::find()
                            ->andWhere('[[author_id]] = [[author]].[[id]]')
                            ->limit(1)
                            ->select('count(*)')
                    ]       
                ]
            ]
        ];
    }

}

Since Yii uses the DI container to create the object, it is also possible to add the behavior globally by defining it in the DI container. Virtual fields allow you to define model attributes as SQL fragments. The advantage of this implementation is that it supports both lazy and greedy loading.

Usage

The library will then take care of everything:

    Author::findByPk(1)->postCount; // Lazy loaded 
    Author::find()->withField('postCount')->one()->postCount; // Greedy loaded

If lazy loading is not implemented and an attribute is used lazily, an exception will be thrown. If greedy loading is not implemented and the field is added to select the normal Yii2 / SQL exception is thrown.

We found these classes greatly helped us in reducing the number of queries (implementing getPostCount() on Author is not ideal).

To maximize compatibility and minimize issues we chose not to use joins, since they can potentially affect the number of records. In a number of cases the resulting query plan could be less than optimal.

We chose not to overload ActiveQuery::select() to support virtual fields. Reason for this is the fact that it changes the semantics of *; * would not by default include all virtual fields.

统计信息

  • 总下载量: 30.31k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 0
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-11-14

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固