yii2 gii CRUD Generator error - Class 'Message

2019-09-12 06:09发布

问题:

I'm creating a basic text only private messaging system for a website using 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. 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).

When I attempt to run the CRUD generator with- Model Class- Message Search Model Class - frontend\models\search\MessageSearch Controller Class - 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.

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 -

--

-- 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 ;

--

-- Constraints for table 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);

回答1:

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.

It should be frontend\models\Message.



回答2:

Take a look into this link.Your Model namespace may be the villain.

Try changing your Model namespace to

use app/models/Message

And I don't think the foreign keys has anything to do with your issue.