cornernote/yii2-linkall

Behavior to handle saving multiple many to many related records in Yii2.

Installs: 80 944

Dependents: 2

Suggesters: 0

Security: 0

Stars: 25

Watchers: 6

Forks: 7

Open Issues: 1

Type:yii2-extension

1.0.0 2015-06-20 08:36 UTC

This package is auto-updated.

Last update: 2024-04-19 18:00:27 UTC


README

Latest Version Software License Build Status Coverage Status Quality Score Total Downloads

Behavior to handle saving multiple many to many related records in Yii2.

Installation

The preferred way to install this extension is through composer.

Either run

$ composer require cornernote/yii2-linkall "*"

or add

"cornernote/yii2-linkall": "*"

to the require section of your composer.json file.

Usage

Post Model

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

class Tag extends ActiveRecord
{

}

Post Controller

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);
    }
}