PHP code example of frostealth / yii2-relation-behavior

1. Go to this page and download the library: Download frostealth/yii2-relation-behavior library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

frostealth / yii2-relation-behavior example snippets


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

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

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

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

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

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

 $categories = ArrayHelper::map(Category::find()->all(), 'id', 'name')