duan4739 2016-08-25 09:03
浏览 292
已采纳

Yii2模型加载异常'ReflectionException',消息'Class require不存在'

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']);
  }
}
  • 写回答

3条回答 默认 最新

  • doufei5537 2016-09-06 09:29
    关注

    So stupid error.

    In model's rules I wrote:

    ['name', 'require'],
    

    And get exception:

    Exception 'ReflectionException' with message 'Class require does not exist'
    
    in /path/to/project/vendor/yiisoft/yii2/di/Container.php:422
    

    But valid name of validator is required - has d at the end.

    IMHO that error is not clearly for understanding.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?