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

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条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么