PHP code example of lucianolima00 / yii2-many-to-many

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

    

lucianolima00 / yii2-many-to-many example snippets


//app\models\User.php


use lucianolima00\ManyToMany\behaviors\ManyToManyBehavior;

class User extends \yii\db\ActiveRecord
{

    /**
     * {@inheritDoc}
     */
    public function behaviors()
    {
        return [
            [
                'class' => ManyToManyBehavior::class(),
                'ownAttribute' => 'user_id', // Name of the column in junction table that represents current model
                'relatedModel' => UserTest::class, // Junction model class
                'attribute' => 'tests', // Represent the attribute of current model 
                'relatedAttribute' => 'test_id', // Name of the column in junction table that represents related model
                'unique' => true // Ensure that for the same ownAttribute only exist one relatedAttribute with same value. Default is true
            ],
        ];
    }

/**
 * Gets query for [[UserTests]].
 * 
 * @return \yii\db\ActiveQuery
 */
public function getUserTests()
{
    return $this->hasMany(UserTest::class(), ['user_id' => 'id']);
}