Mar*_*ick 0 php yii2 yii2-advanced-app
我正在使用Yii2高级版 ; 并有一个问题.
码:
$model = \Yii::createObject($this->modelClass);
if ($model->load($temp, '') && $model->validate()) {
$data = $model;
}
Run Code Online (Sandbox Code Playgroud)
$temp - 是模型数据阵列;
$this->modelClass - 动态模型类名;
代码在cli模式下运行(Yii2控制台应用程序),modelClass- 来自backend应用程序的模型.
它在方法中崩溃了load:
Exception 'ReflectionException' with message 'Class require does not exist'
in /path/to/project/vendor/yiisoft/yii2/di/Container.php:422
Run Code Online (Sandbox Code Playgroud)
这很奇怪,因为模型Brand 有效,但类似的其他模型却没有.但是,模型对象已成功创建.
更新类别模型代码
<?php
namespace backend\models;
/**
* Category model
*/
class Category extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return '{{%category}}';
}
/**
* @inheritdoc
*/
public function attributes()
{
return [
'id',
'parent_id',
'name',
'image',
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => \Yii::t('backend/model', 'ID'),
'parent_id' => \Yii::t('backend/model', 'Parent cat ID'),
'name' => \Yii::t('backend/model', 'Name'),
'image' => \Yii::t('backend/model', 'Image'),
];
}
/**
* @inheritdoc
*/
public function rules()
{
return [
['name', 'require'],
[['name', 'image'], 'string', 'max' => 255],
[['id', 'parent_id'], 'integer'],
['parent_id', 'exist', 'targetAttribute' => 'id', 'skipOnEmpty' => true],
['parent_id', 'default', 'value' => 0],
];
}
/**
* Method select parent category
* @return \yii\db\ActiveQuery
*/
public function getParent()
{
return $this->hasOne(static::className(), ['id' => 'parent_id']);
}
}
Run Code Online (Sandbox Code Playgroud)
这么愚蠢的错误.
我在模型的规则中写道:
['name', 'require'],
Run Code Online (Sandbox Code Playgroud)
得到例外:
Exception 'ReflectionException' with message 'Class require does not exist'
in /path/to/project/vendor/yiisoft/yii2/di/Container.php:422
Run Code Online (Sandbox Code Playgroud)
但是需要验证者的有效名称- d最后.
恕我直言,错误是不明白的理解.