Yii 2 Models - Class Yii dbQuery包含1个抽象方法,因此必须声明为抽象或实现其余方法


Yii 2 Models - Class yiidbQuery contains 1 abstract method and must therefore be declared abstract or implement the remaining methods

我是一个初学者。首先,我使用Gii扩展来生成我的模型和控制器。我已经设置了我的数据库在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)

my model:

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

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

可以通过更新yii2框架来解决。yii.db.Query使用yii.db.QueryTrait,其中indexBy方法实现。请比较您的Query.php, QueryTrait.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