2017 © Pedro Peláez
 

yii2-extension yii2-ar-behaviors

Behaviors for Yii2

image

mdmsoft/yii2-ar-behaviors

Behaviors for Yii2

  • Wednesday, October 5, 2016
  • by mdmunir
  • Repository
  • 4 Watchers
  • 18 Stars
  • 13,961 Installations
  • PHP
  • 7 Dependents
  • 0 Suggesters
  • 10 Forks
  • 3 Open issues
  • 7 Versions
  • 6 % Grown

The README.md

Activerecord Behaviors for Yii2

Installation

The preferred way to install this extension is through composer., (*1)

Either run, (*2)

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

or add, (*3)

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

to the require section of your composer.json file., (*4)

Query Scope

QueryScopeBehavior will automatically attached to ActiveQuery via Yii::$container. You can use this behavior to create query scope from ActiveRecord., (*5)

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

Extend Activerecord with out inheriting :grinning: . This behavior use to merge two table and treated as one ActiveRecord., (*6)

Example: We have model CustomerDetail, (*7)

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

}

and model Customer, (*8)

/**
 * @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, (*9)

$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

Use to save model and its relation., (*10)

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, (*11)

$model = new Order();

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

RelationTrait

Similar with RelationBehavior, (*12)

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, (*13)

$model = new Order();

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

The Versions

05/10 2016

dev-master

9999999-dev

Behaviors for Yii2

  Sources   Download

BSD-3-Clause

The Requires

 

by Misbahul Munir

yii2 behavior activerecord query scope

22/02 2016

1.3

1.3.0.0

Behaviors for Yii2

  Sources   Download

BSD-3-Clause

The Requires

 

by Misbahul Munir

yii2 behavior activerecord query scope

04/02 2016

1.2

1.2.0.0

Behaviors for Yii2

  Sources   Download

BSD-3-Clause

The Requires

 

by Misbahul Munir

yii2 behavior activerecord query scope

04/03 2015

1.0.0

1.0.0.0

Behaviors for Yii2

  Sources   Download

BSD-3-Clause

The Requires

 

by Misbahul Munir

yii2 behavior activerecord query scope

04/03 2015

1.1

1.1.0.0

Behaviors for Yii2

  Sources   Download

BSD-3-Clause

The Requires

 

by Misbahul Munir

yii2 behavior activerecord query scope

02/11 2014

1.0.0-RC2

1.0.0.0-RC2

Behaviors for Yii2

  Sources   Download

BSD-3-Clause

The Requires

 

by Misbahul Munir

yii2 behavior activerecord query scope

02/11 2014

1.0.0-RC

1.0.0.0-RC

Behaviors for Yii2

  Sources   Download

BSD-3-Clause

The Requires

 

by Misbahul Munir

yii2 behavior activerecord query scope