yii2-relation-behavior ¶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:
array
- Required:
true
true
relation will be like $this->hasMany(...)
overwise $this->hasOne(...)
)
- Type:
bool
- Required:
false
- Default:
false
$this->hasMany(...)->onCondition(['status' => Category::STATUS_ACTIVE])
)
- Type:
array|null
- Required:
false
- Default:
null
- Type:
array|string|Closure|null
- Required:
false
- Default:
null
$this->hasMany(...)->via(...)
)
- Type:
string|null
- Required:
false
- Default:
false
- Type:
array|null
- Required:
false
- Default:
[]
$model->manufacturer = ['manufacturer_id' => 12]
)
- Type:
Closure|null
- Required:
false
- Default:
Closure
(Searching by all pk keys)
How to use ¶This behavior will allow you to use related models like usuall without created extra methods like:
`
php
public function getCategories()
{
return $this->hasMany(...);
}
Instead, you just need to declare this in the behavior like on example.__
php
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.**__
php
**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