Yii 2 Models - Class yii\db\Query contains 1 abstract method and must therefore be declared abstract or implement the remaining methods

StackOverflow https://stackoverflow.com/questions/23426299

  •  14-07-2023
  •  | 
  •  

Question

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

Was it helpful?

Solution

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

OTHER TIPS

It may be solved by updating yii2 framework. yii.db.Query uses yii.db.QueryTrait in which indexBy method is implemented. Please compare your Query.php, QueryTrait.php and QueryInterface.php with

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top