【问题标题】:PHP Compile Error while creating Views and Controller using Gii in YII2在 YII2 中使用 Gii 创建视图和控制器时 PHP 编译错误
【发布时间】:2014-11-17 16:50:27
【问题描述】:

我已经为表格创建了 Review 模型。之后,在为同一个表创建视图和控制器时,它会显示 PHP 编译错误。

PHP Compile Error – yii\base\ErrorException

Declaration of app\models\Review::getRelation() must be compatible with yii\db\ActiveRecordInterface::getRelation($name, $throwException = true)

这是完整的错误页面 http://pastebin.com/kf8RFun8. 我已经为我的表创建了其余的 MVC。我只得到这个错误。

我的模型课 应用\模型\评论 搜索模型类 应用\模型\评论搜索 控制器类 app\controllers\ReviewController

注意:在 Yii2-Advanced 中创建相同的内容时,它会显示 Error (#64) Internal Server Error

审查模型:

<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "review".
 *
 * @property string $id
 * @property string $title
 * @property string $reviewer_id
 * @property string $timestamp
 * @property string $description
 * @property string $organization_id
 * @property integer $rating
 * @property string $relation_id
 * @property integer $send_msg
 * @property string $org_contact_email
 * @property string $org_contact_msg
 *
 * @property Answer[] $answers
 * @property Reviewer $reviewer
 * @property Organization $organization
 * @property Relation $relation
 */
class Review extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'review';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['title', 'reviewer_id', 'organization_id', 'rating', 'relation_id'], 'required'],
            [['reviewer_id', 'organization_id', 'rating', 'relation_id', 'send_msg'], 'integer'],
            [['timestamp'], 'safe'],
            [['title'], 'string', 'max' => 45],
            [['description'], 'string', 'max' => 2000],
            [['org_contact_email'], 'string', 'max' => 60],
            [['org_contact_msg'], 'string', 'max' => 1000]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'title' => 'Title',
            'reviewer_id' => 'Reviewer ID',
            'timestamp' => 'Timestamp',
            'description' => 'Description',
            'organization_id' => 'Organization ID',
            'rating' => 'Rating',
            'relation_id' => 'Relation ID',
            'send_msg' => 'Send Msg',
            'org_contact_email' => 'Org Contact Email',
            'org_contact_msg' => 'Org Contact Msg',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getAnswers()
    {
        return $this->hasMany(Answer::className(), ['review_id' => 'id']);
    }

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

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

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

【问题讨论】:

  • 请向我们展示您的Review 型号
  • 我刚刚发现这是因为我的外键关系。没有外键关系,我可以创建 ViewsController 。但不知道我的外键有什么问题。 :(
  • 亲爱的,请重命名你的getRelation方法并给我结果

标签: php compiler-errors crud yii2


【解决方案1】:

您需要重命名您的 getRelation() 方法,因为您将覆盖 yii\db\ActiveRecordInterface::getRelation($name, $throwException = true)。所以这将导致getRelation 方法声明无效的异常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-04
    • 1970-01-01
    • 2022-10-18
    相关资源
    最近更新 更多