0

我是 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

4

2 回答 2

3

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

于 2014-08-25T14:17:49.510 回答
1

可以通过更新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

于 2014-05-02T13:05:05.010 回答