定制 phpshko/yii2-magic-scopes 二次开发

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

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

phpshko/yii2-magic-scopes

最新稳定版本:0.1.0

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)

统计信息

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

GitHub 信息

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

其他信息

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