定制 minity/yii2-model-setup 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

minity/yii2-model-setup

Composer 安装命令:

composer require minity/yii2-model-setup

包简介

Configure all the model aspects in one place.

README 文档

README

If you feel uncomfortable to looking up dozen of methods while adding or checking up a property of Yii2 model, consider to use this traits.

Example

Usually, creating any ActiveRecord model we must define several methods, such tableName(), attributeLabels(), attributeHints(), rules() and fields().

Using \Minity\ModelSetup\ActiveRecordConfigurationTrait all we need is define setup() method as below

<?php

use Minity\ModelSetup\ActiveRecordConfigurationTrait;
use yii\db\ActiveRecord;

class Record extends ActiveRecord
{
    use ActiveRecordConfigurationTrait;
    
    protected static function setup() {
        return [
            'tableName' => '{{%record}}',
            'attributes' => [
                'field1' => [
                    'label' => 'Record Field 1',
                    'hint' => 'Record Hint 1',
                    'toArray' => 'record_field_1',
                    'rules' => ['required', ['number', 'min' => 0]],
                ],
                'field2' => [
                    'label' => 'Record Field 2',
                    'hint' => 'Record Hint 2',
                    'toArray' => true, // the same as 'field2'
                    'rules' => ['string'],
                ],
                'field3' => [
                    'label' => 'Record Field 3',
                    //'toArray' => false, // default
                    'rules' => ['safe'],
                ],
            ],
            'relations' => [
                'rel1' => ['hasOne', AnotherRecord::className(), ['id' => 'field1']],
                'rel2' => ['hasMany', ViaRecord::className(), ['id' => 'via_id'],
                    'viaTable' => ['junction_table', ['rec_id' => 'id']],
                ],
            ]
        ];
    }
}

Pay attention to the relations section. You can see there a declarative way to define relations, so you can get ActiveQuery object by calling $model->getRel1() or get ActiveRecord by calling $model->rel1.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-10-22

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固