nkizza/simplerelations 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

nkizza/simplerelations

Composer 安装命令:

composer require nkizza/simplerelations

包简介

Simplerelations for Yii2.

README 文档

README

Provides simple admin tool to manage relations. Configurate needed relation with RelatedBehavior, add RelatedWidget to your form and enjoy the easy relation management.

Installation

The preferred way to install this extension is through composer.

Either run

composer require nkizza/simplerelations:~1.0

or add

"nkizza/simplerelations" : "~1.0"

to the require section of your application's composer.json file.

Usage

This extension comes with two possibilities:

  1. Behavior that serves to manage related active records. You have to provide a specific attribute for that records and configure validation rules first; then just add the behavior to your model.
  2. Simple widget that serves to display your related records and provide functionality to add, edit and delete them. Widget is simply configurable and lets you create flexible forms for your related records.

RelatedBehavior

First, add the specific public attribute for your related records and include it into your model. Then, configure the behavior:

class Match extends \yii\db\ActiveRecord
{
    public $_players;
	
    public function behaviors() {
        return [
            [
                'class' => \nkizza\simplerelations\RelatedBehavior::className(),
                'attribute' => '_players', //attribute which stores the related data
                'uploadRelation' => 'players', //relation name
                'uploadModelScenario' => 'default', //you can provide the specific scenario name for the related models
                'fields' => [   //fields of related record that we manage using this behavior
                    'id_player', 'min_played', 'goals', 'assists', 'y_cards', 'r_cards',            
                ],
            ],
        ];
    }
	
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['_players'], 'safe'],
        ];
    }
    
    /**
     * @return \yii\db\ActiveQuery
     */
    public function getPlayers()
    {
        return $this->hasMany(PlayerMatch::className(), ['id_match' => 'id']);
    }
    

RelationWidget

Related widget helps to manage related records. It allows you to build flexible forms to edit related records within the current model form.

Example of use
Related widget extends \yii\widgets\InputWidget. There are two ways of using it, with an ActiveForm instance or as a widget setting up its model and attribute.

<?php
use nkizza\simplerelations\RelationWidget;
?>

<h3>Players of match</h3>
<?= RelationWidget::widget([
	//current owner model, required
    'model' => $model,
     //creates a labels for related fields. Useful for table-like view.
    'labels' => true,  
    //attribute to store related widgets, required
    'attribute' => '_players',  
    //relation name, required
    'relation' => 'players',    
    //addititional condition to get related models. Added to relation query, optional
    'condition' => ['home' => 1], 
    //container options, optional
    'options' => ['class' => 'players-table'], 
    //each fields row container options, optional
    'fieldOptions' => ['class' => 'form-inline mb10'], 
    'fields' => [
        [
        	//related record attribute
            'attribute' => 'home', 
            //Html helper method
            'method' => 'hiddenInput',
            //preset value. If not set, related record value is used.
            'value' => 1,  
        ],
        [
            'attribute' => 'id_player',
            'method' => 'dropDownList',
            //items for dropDownList
            'items' => ArrayHelper::map(\app\models\Player::find()->orderBy(['surname' => SORT_ASC])->all(), 'id', 'fullname'),   
        ],
        //simple text field for attribute `min_played`
        'min_played', 
        
        //Wrapper. Usually used to wrap some fields into a dropdown or another container. 
        //You need to configurate it with `html` option and add `{{content}}` variable into your html to place the fields.
        //`fields` option is configurated similar to the whole widget (dropdowns, hidden fields, etc).
        [
        	//wrapper label
            'label' => 'Goals and assists dropdown',
            //required for wrapper method
            'method' => 'wrapper',
            //container options
            'wrapperOptions' => ['class' => 'dropdown form-group goals'],
            //required for wrapper. Wrapper template, don't forget to insert {{content}} somewhere
            'html' => '
                <button id="drop_goals" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" class="btn btn-info">
                    Goals <span data-attr="goals"></span> <span class="caret"></span>
                </button>
                <ul class="dropdown-menu" aria-labelledby="drop_goals">{{content}}</ul>
            ',
            //fields to wrap
            'fields' => [ 
                'goals', 'assists', 
            ]
        ],
        [
            'attribute' => 'y_cards',
            //other simple Html helper methods are allowed too.
            'method' => 'textarea', 
        ],
    ]
]);?>

You can use these classes together or separately. Together they make a complete solution to save simple relations to your model; separately, you need to provide client or server part to save them.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2017-03-05

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固