Yii2 模型加载异常 'ReflectionException',消息 'Class require does not exist'
Yii2 model load Exception 'ReflectionException' with message 'Class require does not exist'
我正在使用 Yii2 进阶版;有问题。
代码:
$model = \Yii::createObject($this->modelClass);
if ($model->load($temp, '') && $model->validate()) {
$data = $model;
}
$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
很奇怪,因为模型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']);
}
}
用容器创建一个对象,你应该设置class,你通过数组创建对象('class'=>$this->modelClass);
如果您尝试从 $this->modelClass
创建 ActiveRecord 对象,其中 modelClass 是动态文本字符串,请尝试以下操作:
$className = (string)$this->modelClass;
$model = new $className;
if ($model->load($temp, '') && $model->validate()) {
$data = $model;
}
modelClass 必须恰好是被实例化的 class 的命名空间。在 $className 分配行上做一个断点,以确保使用的值确实是所需的命名空间。
如此愚蠢的错误。
我在模型规则中写道:
['name', 'require'],
并得到异常:
Exception 'ReflectionException' with message 'Class require does not exist'
in /path/to/project/vendor/yiisoft/yii2/di/Container.php:422
但验证器的有效名称是 必需的 - 末尾有 d
。
恕我直言 错误理解不清晰。
我正在使用 Yii2 进阶版;有问题。
代码:
$model = \Yii::createObject($this->modelClass);
if ($model->load($temp, '') && $model->validate()) {
$data = $model;
}
$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
很奇怪,因为模型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']);
}
}
用容器创建一个对象,你应该设置class,你通过数组创建对象('class'=>$this->modelClass);
如果您尝试从 $this->modelClass
创建 ActiveRecord 对象,其中 modelClass 是动态文本字符串,请尝试以下操作:
$className = (string)$this->modelClass;
$model = new $className;
if ($model->load($temp, '') && $model->validate()) {
$data = $model;
}
modelClass 必须恰好是被实例化的 class 的命名空间。在 $className 分配行上做一个断点,以确保使用的值确实是所需的命名空间。
如此愚蠢的错误。
我在模型规则中写道:
['name', 'require'],
并得到异常:
Exception 'ReflectionException' with message 'Class require does not exist'
in /path/to/project/vendor/yiisoft/yii2/di/Container.php:422
但验证器的有效名称是 必需的 - 末尾有 d
。
恕我直言 错误理解不清晰。