phpshko/yii2-magic-scopes 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

phpshko/yii2-magic-scopes

Composer 安装命令:

composer require phpshko/yii2-magic-scopes

包简介

Yii2 behavior for query magic scopes

README 文档

README

This behavior help you avoid creating QueryClass for your model, if you need only a simple scopes.

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist phpshko/yii2-magic-scopes "dev-master"

or add

"phpshko/yii2-magic-scopes": "dev-master"

to the require section of your composer.json file.

In Gii

You can generate a model using "Magic Scopes Generator". Will be automatically generated needs PHPDoc and overwrite find() method. In this generator, you can select "Creation Mode" and "Save DocBlock to".

Creation Mode:

  • Use Magic Query

         * @method ActiveQuery|UserWithMagic orAddressIdNotLike($addressId)
         * @method ActiveQuery|UserWithMagic orAddressIdNotBetween($from, $to)
         * @method ActiveQuery|UserWithMagic orAddressIdNotMore($than, $include = false)
         * @method ActiveQuery|UserWithMagic orAddressIdNotLess($than, $include = false)
         * @method ActiveQuery|UserWithMagic orAddressId($addressId)
         *
         */
        class UserWithMagic extends \yii\db\ActiveRecord
        {
            /**
             * @inheritdoc
             * @return MagicActiveQuery|UserWithMagic
             */
            public static function find()
            {
                return new MagicActiveQuery(get_called_class());
            }
  • Create Query

        /**
         * This is the model class for table "user".
         *
         * @property integer $id
         * @property string $username
         * @property string $forename
         * @property string $surname
         * @property integer $year
         * @property integer $address_id
         */
        class UserWithCreate extends \yii\db\ActiveRecord
        {
            /**
             * @inheritdoc
             * @return UserWithCreateQuery|UserWithCreate
             */
            public static function find()
            {
                return new UserWithCreateQuery(get_called_class());
            }

    and automatically create query class, attach behavior, and put PhpDoc

    ...
     * @method UserWithCreateQuery|UserWithCreate orAddressIdNot($addressId)
     * @method UserWithCreateQuery|UserWithCreate orAddressIdNotIn($array)
     * @method UserWithCreateQuery|UserWithCreate orAddressIdNotLike($addressId)
     * @method UserWithCreateQuery|UserWithCreate orAddressIdNotBetween($from, $to)
     * @method UserWithCreateQuery|UserWithCreate orAddressIdNotMore($than, $include = false)
     * @method UserWithCreateQuery|UserWithCreate orAddressIdNotLess($than, $include = false)
     * @method UserWithCreateQuery|UserWithCreate orAddressId($addressId)
     *
     */
    class UserWithCreateQuery extends \yii\db\ActiveQuery
    {
        /**
         * @inheritdoc
         */
        public function behaviors()
        {
            return [
                [
                    'class' => \phpshko\magicscopes\MagicScopesBehavior::className()
                ]
            ];
        }
    }
  • Attach Behavior

    ...
     * @method ActiveQuery|UserWithAttach orAddressIdNotIn($array)
     * @method ActiveQuery|UserWithAttach orAddressIdNotLike($addressId)
     * @method ActiveQuery|UserWithAttach orAddressIdNotBetween($from, $to)
     * @method ActiveQuery|UserWithAttach orAddressIdNotMore($than, $include = false)
     * @method ActiveQuery|UserWithAttach orAddressIdNotLess($than, $include = false)
     * @method ActiveQuery|UserWithAttach orAddressId($addressId)
     *
     */
    class UserWithAttach extends \yii\db\ActiveRecord
    {
        /**
         * @inheritdoc
         * @return ActiveQuery|UserWithAttach
         */
        public static function find()
        {
            $query = parent::find();
            $query->attachBehavior('MagicScopesBehavior', MagicScopesBehavior::className());
            return $query;
        }    

If you select "Save DocBlock to MagicAutoComplete.php", all PhpDoc for this model to be merged with the old data in the file MagicAutoComplete.php (in the same folder as the model)

You can see examples of created models with different settings here.

PhpStorm autocomplete

Example:

$queryWith = UserWith::find()
                        ->andId(45)
                        ->andUsernameLike('php')
                        ->andYearBetween(1980, 2000)
                        ->addressIdMore(10);

$queryWithout = UserWithout::find()
                        ->andWhere(['id' => 45])
                        ->andWhere(['like', 'username', 'php'])
                        ->andWhere(['between', 'year', 1980, 2000])
                        ->andWhere(['>', 'address_id', 10]);

More examples you can find in this test

You can not write "and":

UserWith::find()->id(5) == UserWith::find()->andId(5)

Full list allow methods (id for example):

->id($id)
->idIn($array)
->idLike($id)
->idBetween($from, $to)
->idMore($than, $include = false)
->idLess($than, $include = false)
->idNot($id)
->idNotIn($array)
->idNotLike($id)
->idNotBetween($from, $to)
->idNotMore($than, $include = false)
->idNotLess($than, $include = false)
->andId($id)
->andIdIn($array)
->andIdLike($id)
->andIdBetween($from, $to)
->andIdMore($than, $include = false)
->andIdLess($than, $include = false)
->andIdNot($id)
->andIdNotIn($array)
->andIdNotLike($id)
->andIdNotBetween($from, $to)
->andIdNotMore($than, $include = false)
->andIdNotLess($than, $include = false)
->orIdIn($array)
->orIdLike($id)
->orIdBetween($from, $to)
->orIdMore($than, $include = false)
->orIdLess($than, $include = false)
->orIdNot($id)
->orIdNotIn($array)
->orIdNotLike($id)
->orIdNotBetween($from, $to)
->orIdNotMore($than, $include = false)
->orIdNotLess($than, $include = false)
->orId($id)

phpshko/yii2-magic-scopes 适用场景与选型建议

phpshko/yii2-magic-scopes 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 569 次下载、GitHub Stars 达 7, 最近一次更新时间为 2015 年 03 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「extension」 「yii2」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 phpshko/yii2-magic-scopes 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 phpshko/yii2-magic-scopes 我们能提供哪些服务?
定制开发 / 二次开发

基于 phpshko/yii2-magic-scopes 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 569
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 18
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 7
  • Watchers: 7
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-03-15