Frapse Behavior Subset
Behavior Subset, (*1)
Installation
The preferred way to install this extension is through composer., (*2)
Either run, (*3)
php composer.phar require suver/yii2-behavior-subset
or add, (*4)
"suver/yii2-behavior-subset": "*"
to the require section of your composer.json
file., (*5)
Write you behaviors section like this for Many-To-Many, (*6)
public function behaviors()
{
return [
[
'class' => '\suver\behavior\Subset',
'relation' => 'authors', // you relation
'attribute' => 'authors_ids',
]
];
}
/**
* Relation with Other Model
*
* @return \yii\db\ActiveQuery
*/
public function getAuthors()
{
return $this->hasMany(OtherModel::className(), ['id' => 'other_model_id'])->viaTable('this_model_to_other_model', ['this_model_id' => 'id']);
}
Usage
Once the extension is installed, simply use it in your code by :, (*7)
// save relation
$model->authors_ids = [1,2,3,4];
$model->save();
// get realtion
var_dump($model->authors)
yii2-behavior-subset
Behavior subset. Esey work with many to many relations, (*8)