Yii2 LinkAll
, (*1)
Behavior to handle saving multiple many to many related records in Yii2., (*2)
Installation
The preferred way to install this extension is through composer., (*3)
Either run, (*4)
$ composer require cornernote/yii2-linkall "*"
or add, (*5)
"cornernote/yii2-linkall": "*"
to the require
section of your composer.json
file., (*6)
Usage
Post Model, (*7)
class Post extends ActiveRecord
{
public function behaviors()
{
return [
\cornernote\linkall\LinkAllBehavior::className(),
];
}
public function getTags()
{
return $this->hasMany(Tag::className(), ['id' => 'tag_id'])
->viaTable('post_to_tag', ['post_id' => 'id']);
//->via('postToTag');
}
}
Tag Model, (*8)
class Tag extends ActiveRecord
{
}
Post Controller, (*9)
class PostController extends Controller
{
public function actionExample()
{
$post = Post::findOne(1);
$tags = [Tag::findOne(2), Tag::findOne(3)];
$extraColumns = []; // extra columns to be saved to the many to many table
$unlink = true; // unlink tags not in the list
$delete = true; // delete unlinked tags
$post->linkAll('tags', $tags, $extraColumns, $unlink, $delete);
}
}