承接 antonyz89/yii2-many-to-many 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

antonyz89/yii2-many-to-many

Composer 安装命令:

composer require antonyz89/yii2-many-to-many

包简介

Many-to-many ActiveRecord relation for Yii 2 framework. Created By Alexey Rogachev, forked by AntonyZ89

README 文档

README

Implementation of Many-to-many relationship for Yii 2 framework.

Created by arogachev, forked by AntonyZ89

Latest Stable Version Total Downloads Latest Unstable Version License

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist antonyz89/yii2-many-to-many

or add

"antonyz89/yii2-many-to-many": "0.3.*"

to the require section of your composer.json file.

Features

  • Configuring using existing hasMany relations
  • Multiple relations
  • No extra queries. For example, if initially model has 100 related records, after adding just one, exactly one row will be inserted. If nothing was changed, no queries will be executed.
  • Auto filling of editable attribute
  • Validator for checking if the received list is valid

Creating editable attribute

Simply add public property to your ActiveRecord model like this:

/** @var int[] */
public $editableRoles = [];

It will store primary keys of related records during update.

Attaching and configuring behavior

First way is to explicitly specify all parameters:

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', 
                    ],
                ],
            ],
        ];
    }
}

Attribute validation

Add editable attribute to model rules for massive assignment.

public function rules()
{
    ['editableRoles', 'required'],
    ['editableRoles', 'integer'],
    ['editableRoles', 'each', 'skipOnEmpty' => false, 'rule' => [
        'exist', 'skipOnError' => true, 'targetClass' => Role::class, 'targetAttribute' => ['editableRoles' => 'id']
    ]],
}

Or use custom validator:

use antonyz89\ManyToMany\validators\ManyToManyValidator;

public function rules()
{
    ['editableRoles', ManyToManyValidator::class],
}

Validator checks list for being array and containing only primary keys presented in related model. It can not be used without attaching ManyToManyBehavior.

Adding control to view

Add control to view for managing related list. Without extensions it can be done with multiple select:

<?= $form->field($model, 'editableRoles')->dropDownList(Role::getList(), ['multiple' => true]) ?>

Example of getList() method contents (it needs to be placed in User model):

use yii\helpers\ArrayHelper;

/**
 * @return array
 */
public static function getList()
{
    $models = static::find()->orderBy('name')->asArray()->all();

    return ArrayHelper::map($models, 'id', 'name');
}

Global Auto Fill

By default, all ManyToMany relations are auto filled after trigger ActiveRecord::EVENT_AFTER_FIND event, but if you want disable this feature and fill relations manually:

// common\config\bootstrap.php

use antonyz89\ManyToMany\behaviors\ManyToManyBehavior;

ManyToManyBehavior::$enableAutoFill = false;

And set autoFill as true on behaviors when you want enable auto fill on specific model

/**
 * @inheritdoc
 */
public function behaviors()
{
    return [
        [
            'class' => ManyToManyBehavior::class,
            'autoFill' => true # default is true. If false, you should fill manually with $model->fill() method
            'relations' => [
                // ...
            ],
        ],
    ];
}

If $enableAutoFill and autoFill are false , you should call $model->fill() to fill relations manually

<?php // example/_form.php

/* @var $this View */
/* @var $model Example */

// fill relations before load the form
$model->fill();
?>

<div class="example-form">
    <!-- ... -->

统计信息

  • 总下载量: 1.58k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 2
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 16
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2020-02-26

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固