Skip to content

Represents Yii2 m2m junction relation as array field. Stores it's changes as difference without cleaning relation.

License

Notifications You must be signed in to change notification settings

siddthartha/yii2-relational-set

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

yii2-relational-set

Represents Yii2 m2m junction relation as array field. Stores it's changes as difference without cleaning relation.

Installation

The preferred way to install this extension is through composer.

Either run

$ composer require siddthartha/yii2-relational-set

or add

"siddthartha/yii2-relational-set": "*"

to the require section of your composer.json file.

Usage

Host model

class Host extends \yii\db\ActiveRecord
{
    public $_slaves;

    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'sets' => [
                'class' => \siddthartha\behaviors\RelationalSetBehavior::class,
                'attributes' => [
                    '_slaves' => 'slaves',
                ],
            ],
        ];
    }

    /**
     * @return \yii\db\ActiveQueryInterface
     */
    public function getSlaves()
    {
        return $this->hasMany(Slave::class, ['id' => 'id_slave'])
            ->viaTable(HostSlave::tableName(), ['id_host' => 'id'])
            ->indexBy('id');
    }
}

View code example

    <?=$form->field( $model, '_slaves')->checkboxList(/*...*/)?>

Any changes to relation (junction table) will be executed as insert and/or update the needed difference only!

About

Represents Yii2 m2m junction relation as array field. Stores it's changes as difference without cleaning relation.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages