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条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看