我正在使用yii2 advanced为网站创建一个基本的纯文本私人消息系统。
我正在使用gii模型和CRUD生成器,但在创建模型类后遇到了CRUD生成的问题。我想知道消息表上的外键是否存在与users表有一对多关系的问题(即一个用户可以有很多消息)。
当我尝试使用以下方式运行CRUD发生器时 - 模型类 - 消息 搜索模型类 - 前端\ models \ search \ MessageSearch 控制器类 - 前端\ controllers \ MessageController
我收到以下错误 -
“消息”类不存在或语法错误。
Message类肯定存在并且 语法是 根据我的IDE更正。
任何想法可能导致错误?
生成的消息类如下 -
<?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
CREATE TABLE IF NOT NOT EXISTS 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)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
ALTER TABLE message
添加约束message_ibfk_2
外键(to_id
)参考user
(id
),
添加约束message_ibfk_1
外键(from_id
)参考user
(id
);
答案 0 :(得分:2)
在您收到的消息中,语法错误是指您为gii提供的类定义中的错误。所以gii无法使用Message
作为定义找到您的模型。
应为frontend\models\Message
。
答案 1 :(得分:1)