你可以手动添加,
<?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
}
如果您还有其他问题或有任何不明白之处,请告诉我。