duan2428 2014-05-02 10:47
浏览 59
已采纳

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

I am a beginner to YII2. To start of with i have used the Gii extension to generate my model and controller. i have set up my database in the db.php file found in the config folder in the root of my site.

I then used the CRUD generator on that model i previously created in my admin module.

when i now go to the admin module i get this error :

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

I have tried to make the class abstract but this gave the error

Cannot instantiate abstract class app\models\User

What am i doing wrong.

Thanks.

EDIT :

I have upgraded my php on the webserver to 5.5.10 and this has fixed this error, i was running V5.4

  • 写回答

2条回答 默认 最新

  • duanhan5230 2014-08-25 14:17
    关注

    If you use MAMP, It's because of XCache. Please try to disable it in the MAMP preference.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 simulink单相桥式整流电路
  • ¥35 问问51单片机流水灯的代码该怎么写
  • ¥15 关于#百度#的问题:感觉已经将字体段落、字体、页边距、纸张大小、文档网络调成与论文模板一致,为什么黄色部分字体左右的间距还是不一样啊,求私信发文件接收看一下
  • ¥15 stata webuse报错
  • ¥15 TypeError: Cannot read properties of undefined (reading 'status')
  • ¥15 如何利用AI去除图片中的竹架子
  • ¥15 python 写个基金爬取的代码,自动卖出功能
  • ¥15 Linux系统启动不起来
  • ¥15 为什么运行仿真数码管不亮(语言-c语言)
  • ¥15 陈仁良《直升机飞行动力学》小扰动线化方程如何推导