Yii 2模型 - 类yii \ db \ Query包含1个抽象方法,因此必须声明为抽象或实现其余方法

时间:2014-05-02 10:47:47

标签: php yii yii2

我是YII2的初学者。为了开始,我使用Gii扩展来生成我的模型和控制器。我已经在我的站点根目录的config文件夹中找到的db.php文件中设置了我的数据库。

然后我在我以前在我的管理模块中创建的那个模型上使用了CRUD生成器。

当我现在转到管理模块时,我收到此错误:

Class yii\db\Query contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (yii\db\QueryInterface::indexBy)

我的模特:

<?php

namespace app\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use yii\db\ActiveRecord;

/**
 * This is the model class for table "user".
 *
 * @property string $id
 * @property string $name
 * @property string $username
 * @property string $pass
 * @property string $email
 * @property string $user_type
 * @property string $date_joined
 *
 * @property Authassignment $authassignment
 * @property Authitem[] $itemnames
 */
class User extends ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'user';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['user_type'], 'string'],
            [['date_joined'], 'required'],
            [['date_joined'], 'safe'],
            [['name'], 'string', 'max' => 248],
            [['username'], 'string', 'max' => 45],
            [['pass'], 'string', 'max' => 256],
            [['email'], 'string', 'max' => 60],
            [['name', 'username', 'password', 'email', 'user_type', 'date_joined'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => 'Name',
            'username' => 'Username',
            'password' => 'Password',
            'email' => 'Email',
            'user_type' => 'User Type',
            'date_joined' => 'Date Joined',
        ];
    }

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

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getItemnames()
    {
        return $this->hasMany(Authitem::className(), ['name' => 'itemname'])->viaTable('_authassignment', ['userid' => 'id']);
    }

    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    public function search($params)
    {
        $query = $this::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        if (!($this->load($params) && $this->validate())) {
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id' => $this->id,
            'date_joined' => $this->date_joined,
        ]);

        $query->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'username', $this->username])
            ->andFilterWhere(['like', 'pass', $this->pass])
            ->andFilterWhere(['like', 'email', $this->email])
            ->andFilterWhere(['like', 'user_type', $this->user_type]);

        return $dataProvider;
    }
}

我试图让类抽象但是这给出了错误

Cannot instantiate abstract class app\models\User

我做错了什么。

感谢。

编辑:

我已将我在网络服务器上的php升级到5.5.10并修复了此错误,我正在运行V5.4

2 个答案:

答案 0 :(得分:3)

如果你使用MAMP,那是因为XCache。 请尝试在MAMP首选项中禁用它。

答案 1 :(得分:1)

可以通过更新yii2框架来解决。 yii.db.Query使用yii.db.QueryTrait方法,其中indexBy方法已实施。 请将您的Query.phpQueryTrait.phpQueryInterface.php

进行比较

https://github.com/yiisoft/yii2/blob/master/framework/db/Query.php
https://github.com/yiisoft/yii2/blob/master/framework/db/QueryTrait.php
https://github.com/yiisoft/yii2/blob/master/framework/db/QueryInterface.php