mdmsoft/yii2-ar-behaviors
最新稳定版本:1.3
Composer 安装命令:
composer require mdmsoft/yii2-ar-behaviors
包简介
Behaviors for Yii2
README 文档
README
Installation
The preferred way to install this extension is through composer.
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
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
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
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
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(); }
统计信息
- 总下载量: 24.24k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 20
- 点击次数: 1
- 依赖项目数: 7
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2014-08-17