1. Go to this page and download the library: Download antonyz89/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/ */
antonyz89 / yii2-many-to-many example snippets
/** @var int[] */
public $editableRoles = [];
namespace common\models;
use antonyz89\ManyToMany\behaviors\ManyToManyBehavior;
class User extends ActiveRecord {
/** @var int[] */
public $editableRoles = [];
/**
* @inheritdoc
*/
public function behaviors()
{
return [
[
'class' => ManyToManyBehavior::class,
'autoFill' => true # default is true. If false, you should fill manually with $model->fill() method
'relations' => [
[
// Editable attribute name
'editableAttribute' => 'editableRoles',
// Model of the junction table
'modelClass' => UserRole::class,
// Name of the column in junction table that represents current model
'ownAttribute' => 'user_id',
// Related model class
'relatedModel' => Role::class,
// Name of the column in junction table that represents related model
'relatedAttribute' => 'role_id',
],
],
],
];
}
}
use yii\helpers\ArrayHelper;
/**
* @return array
*/
public static function getList()
{
$models = static::find()->orderBy('name')->asArray()->all();
return ArrayHelper::map($models, 'id', 'name');
}
// common\config\bootstrap.php
use antonyz89\ManyToMany\behaviors\ManyToManyBehavior;
ManyToManyBehavior::$enableAutoFill = false;
/**
* @inheritdoc
*/
public function behaviors()
{
return [
[
'class' => ManyToManyBehavior::class,
'autoFill' => true # default is true. If false, you should fill manually with $model->fill() method
'relations' => [
// ...
],
],
];
}
// example/_form.php
/* @var $this View */
/* @var $model Example */
// fill relations before load the form
$model->fill();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.