PHPackages                             phpshko/yii2-magic-scopes - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. phpshko/yii2-magic-scopes

ActiveYii2-extension[Database &amp; ORM](/categories/database)

phpshko/yii2-magic-scopes
=========================

Yii2 behavior for query magic scopes

0.1.0(11y ago)75691MITPHP

Since Mar 15Pushed 11y ago7 watchersCompare

[ Source](https://github.com/phpshko/yii2-magic-scopes)[ Packagist](https://packagist.org/packages/phpshko/yii2-magic-scopes)[ RSS](/packages/phpshko-yii2-magic-scopes/feed)WikiDiscussions master Synced today

READMEChangelog (3)Dependencies (4)Versions (4)Used By (0)

Yii2 behavior for ActiveQuery Magic Scopes
==========================================

[](#yii2-behavior-for-activequery-magic-scopes)

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

Installation
------------

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

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
------

[](#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](https://github.com/phpshko/yii2-magic-scopes/blob/master/tests/unit/models).

[![PhpStorm autocomplete](https://camo.githubusercontent.com/754d49afbd587e973a51355f2bbc0b3c876f80818c5d8e63241fe5b153d9d4c9/687474703a2f2f686162726173746f726167652e6f72672f66696c65732f3135372f3138372f6339302f31353731383763393036346434646361623365393932396532353637323331622e706e67 "PhpStorm autocomplete")](https://camo.githubusercontent.com/754d49afbd587e973a51355f2bbc0b3c876f80818c5d8e63241fe5b153d9d4c9/687474703a2f2f686162726173746f726167652e6f72672f66696c65732f3135372f3138372f6339302f31353731383763393036346434646361623365393932396532353637323331622e706e67)

Example:
--------

[](#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](https://github.com/phpshko/yii2-magic-scopes/blob/master/tests/unit/MagicScopesTest.php)

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)
```

###  Health Score

29

—

LowBetter than 57% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity19

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~7 days

Total

3

Last Release

4113d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/4d29b657e1e4191d5f0f9529d0f9082e134ecbf5ef25d1acb141d8466e65e11a?d=identicon)[phpshko](/maintainers/phpshko)

---

Top Contributors

[![phpshko](https://avatars.githubusercontent.com/u/9990396?v=4)](https://github.com/phpshko "phpshko (14 commits)")

---

Tags

yii2extension

###  Code Quality

TestsCodeception

### Embed Badge

![Health badge](/badges/phpshko-yii2-magic-scopes/health.svg)

```
[![Health](https://phpackages.com/badges/phpshko-yii2-magic-scopes/health.svg)](https://phpackages.com/packages/phpshko-yii2-magic-scopes)
```

###  Alternatives

[craftcms/cms

Craft CMS

3.6k3.6M3.1k](/packages/craftcms-cms)[johnitvn/yii2-ajaxcrud

Gii CRUD template for Single Page Ajax Administration for yii2

105215.6k16](/packages/johnitvn-yii2-ajaxcrud)[dmstr/yii2-migrate-command

Console Migration Command with multiple paths/aliases support

33298.4k6](/packages/dmstr-yii2-migrate-command)[dmstr/yii2-db

Database extensions

19656.2k6](/packages/dmstr-yii2-db)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
