PHPackages                             mdmsoft/yii2-ar-behaviors - 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. mdmsoft/yii2-ar-behaviors

ActiveYii2-extension

mdmsoft/yii2-ar-behaviors
=========================

Behaviors for Yii2

1.3(10y ago)2024.6k↑200%10[2 PRs](https://github.com/mdmsoft/yii2-ar-behaviors/pulls)1BSD-3-ClausePHP

Since Nov 2Pushed 6y ago6 watchersCompare

[ Source](https://github.com/mdmsoft/yii2-ar-behaviors)[ Packagist](https://packagist.org/packages/mdmsoft/yii2-ar-behaviors)[ RSS](/packages/mdmsoft-yii2-ar-behaviors/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (6)Dependencies (1)Versions (7)Used By (1)

Activerecord Behaviors for Yii2
===============================

[](#activerecord-behaviors-for-yii2)

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

[](#installation)

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

Either run

```
php composer.phar require mdmsoft/yii2-ar-behaviors "~1.0"

```

or add

```
"mdmsoft/yii2-ar-behaviors": "~1.0"

```

to the require section of your `composer.json` file.

Query Scope
-----------

[](#query-scope)

`QueryScopeBehavior` will automatically attached to `ActiveQuery` via `Yii::$container`. You can use this behavior to create query scope from `ActiveRecord`.

```
class Sales extends ActiveRecord
{
    ...
    public static function defaultScope($query)
    {
        $query->andWhere(['status' => self::STATUS_OPEN]);
    }

    public static function bigOrder($query, $ammount=100)
    {
        $query->andWhere(['>','ammount',$ammount]);
    }
}

----
// get all opened sales
Sales::find()->all(); // apply defaultScope

// opened sales and order bigger than 200
Sales::find()->bigOrder(200)->all();
```

ExtendedBehavior
----------------

[](#extendedbehavior)

Extend `Activerecord` with out inheriting 😀 . This behavior use to merge two table and treated as one ActiveRecord.

Example: We have model `CustomerDetail`

```
/**
 * @property integer $id
 * @property string $full_name
 * @property string $organisation
 * @property string $address1
 * @property string $address2
 */
class CustomerDetail extends ActiveRecord
{

}
```

and model `Customer`

```
/**
 * @property integer $id
 * @property string $name
 * @property string $email
 */
class Customer extends ActiveRecord
{

    public function behaviors()
    {
        return [
            [
                'class' => 'mdm\behaviors\ar\ExtendedBehavior',
                'relationClass' => CustomerDetail::className(),
                'relationKey' => ['id' => 'id'],
            ],
        ];
    }
}
```

After that, we can access `CustomerDetail` property from `Customer` as their own property

```
$model = new Customer();

$model-name = 'Doflamingo';
$model->organisation = 'Donquixote Family';
$model->address = 'North Blue';

$model->save(); // it will save this model and related model
```

RelationBehavior
----------------

[](#relationbehavior)

Use to save model and its relation.

```
class Order extends ActiveRecord
{
    public function getItems()
    {
        return $this->hasMany(Item::className(),['order_id'=>'id']);
    }

    public function behaviors()
    {
        return [
            [
                'class' => 'mdm\behaviors\ar\RelationBehavior',
                'beforeRSave' => function($item){
                    return $item->qty != 0;
                }
            ],
        ];
    }
}
```

usage

```
$model = new Order();

if($model->load(Yii::$app->request->post()){
    $model->items = Yii::$app->request->post('Item',[]);
    $model->save();
}
```

RelationTrait
-------------

[](#relationtrait)

Similar with RelationBehavior

```
class Order extends ActiveRecord
{
    use \mdm\behavior\ar\RelationTrait;

    public function getItems()
    {
        return $this->hasMany(Item::className(),['order_id'=>'id']);
    }

    public function setItems($value)
    {
        $this->loadRelated('items', $value);
    }

    public function beforeRSave($item)
    {
        return $item->qty != 0;
    }

}
```

usage

```
$model = new Order();

if($model->load(Yii::$app->request->post()){
    $model->items = Yii::$app->request->post('Item',[]);
    $model->save();
}
```

###  Health Score

39

—

LowBetter than 86% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity37

Limited adoption so far

Community19

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 94.9% 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 ~95 days

Recently: every ~119 days

Total

6

Last Release

3733d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/9a5ba97416867dd2bff6c4e2fcd0e52f8b1f8bb7521dcef6b734636009ce834e?d=identicon)[mdmunir](/maintainers/mdmunir)

---

Top Contributors

[![mdmunir](https://avatars.githubusercontent.com/u/5828252?v=4)](https://github.com/mdmunir "mdmunir (56 commits)")[![makeen](https://avatars.githubusercontent.com/u/176375?v=4)](https://github.com/makeen "makeen (2 commits)")[![luckynvic](https://avatars.githubusercontent.com/u/4830453?v=4)](https://github.com/luckynvic "luckynvic (1 commits)")

---

Tags

yii2Behavioractiverecordquery scope

### Embed Badge

![Health badge](/badges/mdmsoft-yii2-ar-behaviors/health.svg)

```
[![Health](https://phpackages.com/badges/mdmsoft-yii2-ar-behaviors/health.svg)](https://phpackages.com/packages/mdmsoft-yii2-ar-behaviors)
```

###  Alternatives

[voskobovich/yii2-linker-behavior

This behavior makes it easy to maintain many-to-many and one-to-many relations in your ActiveRecord models.

80319.0k9](/packages/voskobovich-yii2-linker-behavior)[yii-dream-team/yii2-lockable-activerecord

Pessimistic locking behavior for Yii2 ActiveRecord

1886.5k](/packages/yii-dream-team-yii2-lockable-activerecord)[nhkey/yii2-activerecord-history

Storage history of changes to ActiveRecord

46143.4k1](/packages/nhkey-yii2-activerecord-history)[mdmsoft/yii2-autonumber

Auto number extension for the Yii framework

1830.9k](/packages/mdmsoft-yii2-autonumber)[apexwire/yii2-restclient

Tools to use API as ActiveRecord for Yii2

143.5k](/packages/apexwire-yii2-restclient)[liyunfang/yii2-upload-behavior

Upload behavior for Yii 2

161.7k](/packages/liyunfang-yii2-upload-behavior)

PHPackages © 2026

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