2017 © Pedro Peláez
 

yii2-extension yii2-relation-behavior

Easy linking and sync relationships many-to-many and one-to-many

image

frostealth/yii2-relation-behavior

Easy linking and sync relationships many-to-many and one-to-many

  • Tuesday, April 12, 2016
  • by frostealth
  • Repository
  • 1 Watchers
  • 4 Stars
  • 705 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 8 Versions
  • 4 % Grown

The README.md

Yii2 Relation Behavior

Easy linking and sync relationships many-to-many., (*1)

Installation

Run the Composer command to install the latest stable version:, (*2)

composer require frostealth/yii2-relation-behavior @stable

Behaviors

  • SyncRelationBehavior
  • EasyRelationBehavior

Usage

SyncRelationBehavior

Attach the behavior to your model:, (*3)

public function behaviors()
{
    return [
        SyncRelationBehavior::className(),
    ];
}

Use the sync method to construct many-to-many associations. The sync method accepts an array of IDs., (*4)

$model->sync('categories', [2, 5, 9]);

EasyRelationBehavior

The EasyRelationBehavior extends the SyncRelationBehavior., (*5)

Attach the behavior to your model:, (*6)

public function behaviors()
{
    return [
        [
            'class' => EasyRelationBehavior::className(),
            'relations' => ['categories'],
            'suffix' => 'ids', // by default
        ],
    ];
}

public function rules()
{
    return [
        ['categoriesIds', 'each', 'rule' => ['integer', 'integerOnly' => true]],
    ];
}

Just add your $suffix to the relation name and you will get associated ids:, (*7)

$categoriesIds = $model->categoriesIds; // [1, 3, 4]

// linking
$categoriesIds = [2, 3, 5];
$model->categoriesIds = $categoriesIds;
$model->save();

Add control to view for managing related list., (*8)

Without extensions it can be done with multiple select:, (*9)

<?php $categories = ArrayHelper::map(Category::find()->all(), 'id', 'name') ?>
<?= $form->field($model, 'categoriesIds')->dropDownList($categories, ['multiple' => true]) ?>

License

The MIT License (MIT). See LICENSE.md for more information., (*10)

The Versions

12/04 2016

dev-master

9999999-dev https://github.com/frostealth/yii2-relation-behavior

Easy linking and sync relationships many-to-many and one-to-many

  Sources   Download

MIT

The Requires

 

yii2 behavior many-to-many relation one-to-many

12/04 2016

0.2.4

0.2.4.0 https://github.com/frostealth/yii2-relation-behavior

Easy linking and sync relationships many-to-many and one-to-many

  Sources   Download

MIT

The Requires

 

yii2 behavior many-to-many relation one-to-many

13/11 2015

0.2.3

0.2.3.0 https://github.com/frostealth/yii2-relation-behavior

Easy linking and sync relationships many-to-many

  Sources   Download

MIT

The Requires

 

yii2 behavior many-to-many relation

13/11 2015

0.2.2

0.2.2.0 https://github.com/frostealth/yii2-relation-behavior

Easy linking and sync relationships many-to-many

  Sources   Download

MIT

The Requires

 

yii2 behavior many-to-many relation

10/11 2015

0.2.1

0.2.1.0 https://github.com/frostealth/yii2-relation-behavior

Easy linking and sync relationships one-to-many

  Sources   Download

MIT

The Requires

 

yii2 behavior relation one-to-many

10/11 2015

0.2.0

0.2.0.0 https://github.com/frostealth/yii2-relation-behavior

Easy linking and sync relationships one-to-many

  Sources   Download

MIT

The Requires

 

yii2 behavior relation one-to-many

06/11 2015

0.1.1

0.1.1.0 https://github.com/frostealth/yii2-relation-behavior

Easy linking relationships one-to-many

  Sources   Download

MIT

The Requires

 

yii2 behavior relation one-to-many

06/11 2015

0.1.0

0.1.0.0 https://github.com/frostealth/yii2-relations-behavior

Easy linking relationships one-to-many

  Sources   Download

MIT

The Requires

 

yii2 behavior relation one-to-many