我正在使用 yii2 advanced 为网站创建一个基本的纯文本私人消息系统。
我正在使用 gii 模型和 CRUD 生成器,但在创建模型类后遇到了 CRUD 生成问题。我想知道消息表上与用户表具有一对多关系的外键是否存在问题(即一个用户可以有很多消息)。
当我尝试使用 - Model Class- Message Search Model Class - frontend\models\search\MessageSearch Controller Class - frontend\controllers\MessageController 运行 CRUD 生成器时
我收到以下错误-
类“消息”不存在或存在语法错误。
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
如果不存在则创建表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非空,主键(id
),键from_id
(from_id
),键to_id
(to_id
))引擎=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=7;
--
-- 表格约束message
ALTER TABLEmessage
添加约束外message_ibfk_2
键(to_id
)引用user
(id
),添加约束外message_ibfk_1
键(from_id
)引用user
(id
);