Download the PHP package ed-smartass/yii2-relation-behavior without Composer
On this page you can find all versions of the php package ed-smartass/yii2-relation-behavior. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ed-smartass/yii2-relation-behavior
More information about ed-smartass/yii2-relation-behavior
Files in ed-smartass/yii2-relation-behavior
Package yii2-relation-behavior
Short Description Behavior for gettting and saving ActiveRecord relations
License MIT
Informations about the package yii2-relation-behavior
yii2-relation-behavior
Installation
Example
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:
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:
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
Limitation
This behavior will successfully save only truly basic relations
- One to many
- Many to one
- Many to many