ed-smartass / yii2-relation-behavior
Behavior for gettting and saving ActiveRecord relations
Installs: 111
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 0
Open Issues: 0
Type:yii2-extension
Requires
- yiisoft/yii2: ~2.0.14
This package is auto-updated.
Last update: 2024-10-22 05:12:09 UTC
README
Installation
composer require ed-smartass/yii2-relation-behavior
Example
use Smartass\Yii2RelationBehavior\RelationBehavior; // ... /** * @inheritdoc */ public function behaviors() { return [ // ... 'relation' => [ 'class' => RelationBehavior::class, 'relations' => [ // Many to one relation 'manufacturer' => [ 'target' => Manufacturer::class, 'link' => ['manufacturer_id' => 'manufacturer_id'] ], // One to many relation 'modelCategories' => [ 'target' => ModelCategories::class, 'link' => ['model_id' => 'model_id'], 'multiple' => true ], // Many to many relation 'categories' => [ 'target' => Category::class, 'link' => ['category_id' => 'category_id'], 'multiple' => true, 'via' => 'modelCategories' ] ] ] // ... ]; } // ...
Relation settings
-
target — target class
- Type:
string
- Required:
true
- Type:
-
link — link condition (same as native yii2 declaration)
- Type:
array
- Required:
true
- Type:
-
multiple — is multiple relation or not (if
true
relation will be like$this->hasMany(...)
overwise$this->hasOne(...)
)- Type:
bool
- Required:
false
- Default:
false
- Type:
-
onCondition — linking condition (will expand to
$this->hasMany(...)->onCondition(['status' => Category::STATUS_ACTIVE])
)- Type:
array|null
- Required:
false
- Default:
null
- Type:
-
filter — extra linking filter
- Type:
array|string|Closure|null
- Required:
false
- Default:
null
- Type:
-
via — name of junction relation (will expand to
$this->hasMany(...)->via(...)
)- Type:
string|null
- Required:
false
- Default:
false
- Type:
-
extraColumns — extra column for linking many to many relations
- Type:
array|null
- Required:
false
- Default:
[]
- Type:
-
find - Callback for searching related record from array (if you set relation value
$model->manufacturer = ['manufacturer_id' => 12]
)- Type:
Closure|null
- Required:
false
- Default:
Closure
(Searching by all pk keys)
- Type:
How to use
This behavior will allow you to use related models like usuall without created extra methods like:
public function getCategories() { return $this->hasMany(...); }
Instead, you just need to declare this in the behavior like on example.__ In addition, this behavior can create or save changes to the related model. To do this, behavior uses a transaction. So you can do this:
$model->categories = [ ['category_id' => 1], // Will find (or create if not exists) category with category_id `1` ['category_id' => 2, 'name' => 'New name'], // Will find (or create) category with category_id `1` and change name ['name' => 'New category'] // Will create new category ]; $model->save();
Each related model will be validated before saving. If it fails, the transaction will be canceled.__
Don't foget add relations to model rules
as safe
/** * @inheritdoc */ public function rules() { return [ // ... ['categories', 'safe'], // ... ]; }
Limitation
This behavior will successfully save only truly basic relations
- One to many
- Many to one
- Many to many