nkizza/simplerelations

为 Yii2 提供的 simplerelations。

安装: 75

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 0

开放问题: 0

类型:yii2-extension

1.0 2017-03-05 16:08 UTC

This package is not auto-updated.

Last update: 2024-09-18 20:09:37 UTC


README

提供简单的管理关系的后台工具。使用 RelatedBehavior 配置所需的关系,将 RelatedWidget 添加到您的表单中,并享受简单的关系管理。

安装

安装此扩展的首选方式是通过 composer

运行以下命令:

composer require nkizza/simplerelations:~1.0

或将以下内容添加到您的应用程序 composer.json 文件的 require 部分中:

"nkizza/simplerelations" : "~1.0"

Usage

此扩展提供两种使用方式

用于管理相关活动记录的行为。您需要为这些记录提供特定的属性并首先配置验证规则;然后只需将行为添加到您的模型中。

  1. Simple widget
  2. Simple widget 是用于显示相关记录并提供添加、编辑和删除它们的功能的小部件。小部件简单易配置,让您为相关记录创建灵活的表单。

RelatedBehavior

首先,为您的相关记录添加特定的公共属性并将其包含到您的模型中。然后,配置行为

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

相关小部件有助于管理相关记录。它允许您在当前模型表单内构建灵活的表单来编辑相关记录。

使用示例
相关小部件扩展 \yii\widgets\InputWidget。有两种使用它的方法,使用 ActiveForm 实例或作为设置其 modelattribute 的小部件。

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

您可以使用这些类一起或单独使用。一起使用时,它们构成一个完整的解决方案,可以将简单的关系保存到模型中;单独使用时,您需要提供客户端或服务器部分来保存它们。