I'm using Yii2 advanced; and have a problem.
Code:
$model = \Yii::createObject($this->modelClass);
if ($model->load($temp, '') && $model->validate()) {
$data = $model;
}
$temp
- is model data array;
$this->modelClass
- dynamic model classname;
Code runs in cli mode (Yii2 console application), modelClass
- model from backend
application.
And it's crashed in load
method:
Exception 'ReflectionException' with message 'Class require does not exist'
in /path/to/project/vendor/yiisoft/yii2/di/Container.php:422
It's weird, because model Brand
works, but similar other models do not. However the model objects are created successfully.
UPDATE Category model code
<?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']);
}
}