PHP code example of voskobovich / yii2-many-many-behavior

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

    

voskobovich / yii2-many-many-behavior example snippets


public function getAuthors()
{
    return $this->hasMany(Author::className(), ['id' => 'author_id'])
                ->viaTable('book_has_author', ['book_id' => 'id']);
}

public function getReviews()
{
    return $this->hasMany(Review::className(), ['id' => 'review_id']);
}

public function behaviors()
{
    return [
        [
            'class' => \voskobovich\behaviors\ManyToManyBehavior::className(),
            'relations' => [
                'author_ids' => 'authors',
				'review_ids' => 'reviews',
            ],
        ],
    ];
}

public function rules()
{
    return [
        [['author_ids', 'review_ids'], 'each', 'rule' => ['integer']]
    ];
}

<?= $form->field($model, 'author_ids')
    ->dropDownList($authorsAsArray, ['multiple' => true]) 

//...
'author_ids' => [
    'authors',
    'fields' => [
        'json' => [
            'get' => function($value) {
                //from internal representation (array) to user type
                return JSON::encode($value);
            },
            'set' => function($value) {
                //from user type to internal representation (array)
                return JSON::decode($value);
            },
        ],
        'string' => [
            'get' => function($value) {
                //from internal representation (array) to user type
                return implode(',', $value);
            },
            'set' => function($value) {
                //from user type to internal representation (array)
                return explode(',', $value);
            },
        ],
    ],
]
//...

...
'author_ids' => [
    'authors',
    'viaTableValues' => [
        'status_key' => BookHasAuthor::STATUS_ACTIVE,
        'created_at' => function() {
            return new \yii\db\Expression('NOW()');
        },
        'is_main' => function($model, $relationName, $attributeName, $relatedPk) {
            return array_search($relatedPk, $model->author_ids) === 0;
        },
    ],
]
...

...
'review_ids' => [
    'reviews',
    'default' => 17,
],
...

...
'review_ids' => [
    'reviews',
    'default' => function($model, $relationName, $attributeName) {
        //default value calculation
        //...
        return $defaultValue;
    },
],
...

function($model, $relationName, $attributeName) {
    //get db connection from primary model (Book)
    $connection = $model::getDb();
    ...
    //OR get db connection from secondary model (Review)
    $secondaryModelClass = $model->getRelation($relationName)->modelClass;
    $connection = $secondaryModelClass::getDb();
    ...
    //further value calculation logic (db query)

    public function behaviors()
    {
        return [
            'manyToMany' => [
                'class' => ManyToManyBehavior::className(),
                'relations' => [
                    'rawMaterialPicturesList' => [
                        'rawMaterialPictures',
                        'viaTableValues' => [
                            'type_key' => 'RAW_MATERIAL_PICTURES',
                        ],
                        'customDeleteCondition' => [
                            'type_key' => 'RAW_MATERIAL_PICTURES',
                        ],
                    ],
                    'molecularStructureList' => [
                        'molecularStructure',
                        'viaTableValues' => [
                            'type_key' => 'MOLECULAR_STRUCTURE',
                        ],
                        'customDeleteCondition' => [
                            'type_key' => 'MOLECULAR_STRUCTURE',
                        ],
                    ],
                ],
            ],
        ];
    }
    
    public function getRawMaterialPictures()
    {
        return $this->hasMany(Attachment::className(), ['id' => 'related_id'])
            ->viaTable('sample_attachments', ['current_id' => 'id'], function ($query) {
                $query->andWhere([
                    'type_key' => 'RAW_MATERIAL_PICTURES',
                ]);
                return $query;
            });
    }
    
    public function getMolecularStructure()
    {
        return $this->hasMany(Attachment::className(), ['id' => 'related_id'])
            ->viaTable('sample_attachments', ['current_id' => 'id'], function ($query) {
                $query->andWhere([
                    'type_key' => 'MOLECULAR_STRUCTURE',
                ]);
                return $query;
            });
    }