yii2 gii CRUD生成器错误-类'消息'不存在或具有语法错误 [英] yii2 gii CRUD Generator error - Class 'Message' does not exist or has syntax error

查看:423
本文介绍了yii2 gii CRUD生成器错误-类'消息'不存在或具有语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用yii2 Advanced为网站创建一个基本的纯文本私人消息传递系统.

I'm creating a basic text only private messaging system for a website using yii2 advanced.

我正在使用gii模型和CRUD生成器,但是在创建模型类之后,CRUD生成遇到了问题.我想知道消息表上与用户表具有一对多关系的外键(即一个用户可以有多条消息)是否可能是一个问题.

I'm using the gii model and CRUD generators but have encountered a problem with the CRUD generation after I have created the model class. I'm wondering whether it might be an issue with the foreign keys on the message table which have a one to many relationship with the users table (i.e. one user can have many messages).

当我尝试使用以下命令运行CRUD生成器时- 型号类别-消息 搜索模型类-frontend \ models \ search \ MessageSearch 控制器类-frontend \ controllers \ MessageController

When I attempt to run the CRUD generator with- Model Class- Message Search Model Class - frontend\models\search\MessageSearch Controller Class - frontend\controllers\MessageController

我收到以下错误-

消息"类不存在或存在语法错误.

Class 'Message' does not exist or has syntax error.

Message类肯定存在,并且 语法是 根据我的IDE正确.

The Message class definitely exists and the syntax is correct according to my IDE.

有什么想法可能导致错误?

Any ideas what might be causing the error?

生成的消息类如下-

<?php
namespace frontend\models;

use Yii;

/**
* This is the model class for table "message".
*
* @property integer $id
* @property string $title
* @property string $message
* @property integer $from_id
* @property integer $to_id
* @property integer $from_viewed
* @property integer $to_viewed
* @property integer $from_deleted
* @property integer $to_deleted
* @property string $from_vdate
* @property string $to_vdate
* @property string $from_ddate
* @property string $to_ddate
* @property string $created
*
* @property User $to
* @property User $from
*/
class Message extends \yii\db\ActiveRecord
{
/**
 * @inheritdoc
 */
public static function tableName()
{
    return 'message';
}

/**
 * @inheritdoc
 */
public function rules()
{
    return [
        [['message', 'from_id', 'to_id', 'created'], 'required'],
        [['message'], 'string'],
        [['from_id', 'to_id', 'from_viewed', 'to_viewed', 'from_deleted', 'to_deleted'], 'integer'],
        [['from_vdate', 'to_vdate', 'from_ddate', 'to_ddate', 'created'], 'safe'],
        [['title'], 'string', 'max' => 255]
    ];
}

/**
 * @inheritdoc
 */
public function attributeLabels()
{
    return [
        'id' => 'ID',
        'title' => 'Title',
        'message' => 'Message',
        'from_id' => 'From ID',
        'to_id' => 'To ID',
        'from_viewed' => 'From Viewed',
        'to_viewed' => 'To Viewed',
        'from_deleted' => 'From Deleted',
        'to_deleted' => 'To Deleted',
        'from_vdate' => 'From Vdate',
        'to_vdate' => 'To Vdate',
        'from_ddate' => 'From Ddate',
        'to_ddate' => 'To Ddate',
        'created' => 'Created',
    ];
}

/**
 * @return \yii\db\ActiveQuery
 */
public function getTo()
{
    return $this->hasOne(User::className(), ['id' => 'to_id']);
}

/**
 * @return \yii\db\ActiveQuery
 */
public function getFrom()
{
    return $this->hasOne(User::className(), ['id' => 'from_id']);
}
}

表sql是-

--

-表message

的表结构

如果不存在则创建表message( id int(11)NOT NULL AUTO_INCREMENT, title varchar(255)默认为空, message文字NOT NULL, from_id int(11)非空, to_id int(11)非空, from_viewed tinyint(1)非空默认值'0', to_viewed tinyint(1)非空默认值'0', from_deleted tinyint(1)非空默认值'0', to_deleted tinyint(1)非空默认值'0', from_vdate datetime DEFAULT NULL, to_vdate datetime DEFAULT NULL, from_ddate datetime DEFAULT NULL, to_ddate datetime DEFAULT NULL, created datetime NOT NULL, 主键(id), 键from_id(from_id), 键to_id(to_id) )ENGINE = InnoDB DEFAULT CHARSET = latin1 AUTO_INCREMENT = 7;

-- Table structure for table message

CREATE TABLE IF NOT EXISTS message ( id int(11) NOT NULL AUTO_INCREMENT, title varchar(255) DEFAULT NULL, message text NOT NULL, from_id int(11) NOT NULL, to_id int(11) NOT NULL, from_viewed tinyint(1) NOT NULL DEFAULT '0', to_viewed tinyint(1) NOT NULL DEFAULT '0', from_deleted tinyint(1) NOT NULL DEFAULT '0', to_deleted tinyint(1) NOT NULL DEFAULT '0', from_vdate datetime DEFAULT NULL, to_vdate datetime DEFAULT NULL, from_ddate datetime DEFAULT NULL, to_ddate datetime DEFAULT NULL, created datetime NOT NULL, PRIMARY KEY (id), KEY from_id (from_id), KEY to_id (to_id) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

-

更改表message 添加约束message_ibfk_2外键(to_id)参考user(id), 添加约束message_ibfk_1外键(from_id)参考user(id);

ALTER TABLE message ADD CONSTRAINT message_ibfk_2 FOREIGN KEY (to_id) REFERENCES user (id), ADD CONSTRAINT message_ibfk_1 FOREIGN KEY (from_id) REFERENCES user (id);

推荐答案

在该消息中,语法错误是指为gii提供的类定义中的错误.因此gii无法使用Message作为定义来找到您的模型.

In that message you're getting, syntax error refers to an error in the class definition you're providing for gii. So gii is unable to find your model using Message as definition.

应为frontend\models\Message.

这篇关于yii2 gii CRUD生成器错误-类'消息'不存在或具有语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆