【问题标题】:how to generate Function yii2 model in master master model如何在主主模型中生成函数 yii2 模型
【发布时间】:2017-05-25 11:34:10
【问题描述】:

我正在使用 yii2 高级应用程序,我创建了一个主模型,我的目标是创建与子模型相同的功能并将它们放入主模型中自动生成而无需返回 gii 工具。 我是这样开始的:

<?php

namespace common\components;

use Yii;

class BaseModel extends \yii\db\ActiveRecord
{

    public static function tableName()
    {
        return  '{{%'.Yii::$app->controller->id.'}}';
    }

    public function rules()
    {       

    }

    public function attributesLabels()
    {       

    }
    ...etc
}

知道如何生成 rules() 和 attributeLabels() 函数吗? 我认为这和生成器一样,但我不知道如何开始。

【问题讨论】:

    标签: inheritance model-view-controller yii2 extend


    【解决方案1】:

    在 Yii2 中这样做并不是一个好主意。我的意思是,如果您从不是 UserController 的控制器中使用 User 模型,您将引用与它无关的表。

    例如,使用相同的User 模型类,当您尝试使用默认的site/login 控制器/操作登录到您的站点时,您的User 模型将尝试在site 表。它会崩溃和燃烧。

    rules() 方法也是如此。有时需要添加无法从数据库表中推断出来的验证规则,需要程序员为模型定义。

    在某些情况下,当您的特定要求与您正在尝试做的事情相匹配时,这似乎是个好主意,但这是一个非常糟糕的做法。

    【讨论】:

    • 对于模型用户,我不会扩展它,但对于其他人,它是可能的,当我需要其他验证规则时,我将不得不覆盖子模型中的功能规则。
    • 如果您需要在控制器中使用不完全相同名称的模型?它可能适用于简单的 CRUDS,但是对于比这更复杂的东西,它仍然是一个非常糟糕的主意。
    【解决方案2】:

    你可以手动添加,

    <?php
    
    namespace common\models;//Change it, that's correct one
    
    use Yii;
    
    class BaseModel extends \yii\db\ActiveRecord
    
    {
    
       public static function tableName()
       {
           return  '{{%'.Yii::$app->controller->id.'}}';
       }
    
       public function rules()
       {       
          return [
              // some base rules
              [['name', 'email', 'subject', 'body'], 'required'],
          ];
       }
    
       public function attributeLabels()
       {
           //Some dummy labels
           return [
               'name' => 'Your name',
               'email' => 'Your email address',
               'subject' => 'Subject',
               'body' => 'Content',
           ];
       }
        ...etc
    }
    

    您实际上可以删除您的 rules()attributeLabels() 以便动态传递(规则将为空)和属性,正如它在文档中所说, By default, attribute labels are automatically generated from attribute names. The generation is done by the method yii\base\Model::generateAttributeLabel(). It will turn camel-case variable names into multiple words with the first letter in each word in upper case. For example, username becomes Username, and firstName becomes First Name.Link to it.

    或者,如果您愿意,您可以在 Base 模型中添加一些基本人员,然后在您的子模型中完成其余的工作 像这样:

    <?php
    
    namespace frontend\models;
    
    use Yii;
    use common\models\BaseModel;
    use yii\helpers\ArrayHelper;
    
    class BaseModel extends BaseModel
    
    {
       public function rules()
       {
           //Get you base model rules, there will be only those
           //that are acceptable for all of your models
           $rules = parent::rules();
           //merge them with new ones, in this example
           //we have property `group` with `string` validator
           //if you want to override your basic rules pass it as second
           //param
           return ArrayHelper::merge($rules, [[['group'], 'string']]);
       }
    
       public function attributeLabels()
       {
           //same is here
           $attributeLabels = parent::attributeLabels();
           return ArrayHelper::merge($attributeLabels, ['group' => 'Group name']);
    
          // OR (use only one) for only new once
          $attributeLabels = parent::attributeLabels();
          $attributeLabels['group'] = 'Some new name';
          return $attributeLabels;
       }
        ...etc
    }
    

    如果您还有其他问题或有任何不明白之处,请告诉我。

    【讨论】:

    • 不,这不是我搜索的内容,我的函数规则必须是动态的,因为我将从 Basemodel 扩展的所有子模型中删除它。结论我有 5 个模型,所以我将有 5 个规则函数,mu 的目的是放置 1 个适用于所有模型的函数以进行代码代码优化。就像我在做的那样:function tableName() 而不是这个 public static function tableName() { return '{{%personne}}'; } 你明白我的意思吗??
    • 你给我的解决方案是针对子模型,而不是从 yii/db/activrecord 扩展的主模型
    猜你喜欢
    • 2015-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-20
    • 1970-01-01
    • 1970-01-01
    • 2013-10-31
    • 2019-07-05
    相关资源
    最近更新 更多