PHP code example of thtmorais / yii2-many-2-many

1. Go to this page and download the library: Download thtmorais/yii2-many-2-many 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/ */

    

thtmorais / yii2-many-2-many example snippets


use thtmorais\many2many\Many2ManyBehavior;

class Model extends \yii\db\ActiveRecord
{
    public function behaviors()
    {
        return [
            [
                'class' => Many2ManyBehavior::class,
                'id' => 'id',
                'attribute' => 'relations',
                'relatedModel' => RelatedModel::class,
                'relatedModelId' => 'id',
                'relatedModelAttribute' => 'model_id',
                'relatedModelValidate' => true
            ],
        ];
    }
}

$model = new Model();

$model->relations = [
    ['id' => 1, 'name' => 'Item 1'],
    ['id' => 2, 'name' => 'Item 2']
];

$model->save();