简体   繁体   English

yii2 gii CRUD生成器错误-类'消息'不存在或具有语法错误

[英]yii2 gii CRUD Generator error - Class 'Message' does not exist or has syntax error

I'm creating a basic text only private messaging system for a website using yii2 advanced. 我正在使用yii2 Advanced为网站创建一个基本的纯文本私人消息传递系统。

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. 我正在使用gii模型和CRUD生成器,但是在创建模型类后,CRUD生成遇到了问题。 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 (ie one user can have many messages). 我想知道消息表上与用户表具有一对多关系的外键是否可能是个问题(即一个用户可以有很多消息)。

When I attempt to run the CRUD generator with- Model Class- Message Search Model Class - frontend\\models\\search\\MessageSearch Controller Class - frontend\\controllers\\MessageController 当我尝试运行CRUD生成器时-模型类-消息搜索模型类-frontend \\ models \\ search \\ MessageSearch控制器类-frontend \\ controllers \\ MessageController

I receive the following error - 我收到以下错误 -

Class 'Message' does not exist or has syntax error. 类“消息”不存在或存在语法错误。

The Message class definitely exists and the syntax is correct according to my IDE. 根据我的IDE,Message类肯定存在,并且语法正确。

Any ideas what might be causing the error? 有什么想法可能导致错误吗?

The generated message class is as follows - 生成的消息类如下:

<?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']);
}
}

The table sql is - 表sql是-

--

-- Table structure for table message -表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创建表( id int(11)NOT NULL AUTO_INCREMENT, title varchar(255)DEFAULT NULL, message文本NOT NULL, from_id int(11)NOT NULL, to_id int(11)NOT NULL, 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日期时间DEFAULT NULL, to_ddate日期时间DEFAULT NULL, created日期时间NOT NULL,主键( id ),KEY from_idfrom_id ),KEY to_idto_id ))ENGINE = InnoDB DEFAULT CHARSET = latin1 AUTO_INCREMENT = 7;

-- -

-- Constraints for table message -表message约束

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 ); ALTER TABLE message ADD CONSTRAINT message_ibfk_2 FOREIGN KEY( to_id )参考userid ),ADD CONSTRAINT message_ibfk_1 FOREIGN KEY( from_id )REFERENCES userid );

In that message you're getting, syntax error refers to an error in the class definition you're providing for gii. 在您得到的消息中,语法错误是指您为gii提供的类定义中的错误。 So gii is unable to find your model using Message as definition. 因此gii无法使用Message作为定义来找到您的模型。

It should be frontend\\models\\Message . 它应该是frontend\\models\\Message

Take a look into this link .Your Model namespace may be the villain. 看一下此链接。您的 Model命名空间可能是坏人。

Try changing your Model namespace to 尝试将您的Model名称空间更改为

use app/models/Message

And I don't think the foreign keys has anything to do with your issue. 而且我认为外键与您的问题无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM