Call to a member function andFilterWhere() on array

I tried to use query builder in my modelSearch to generate gridview, but i got this error




    public function searchcoursegroupings($params,$user=null)


   $query = (new \yii\db\Query())

      ->select (['cs.academic_semester_id', '', 'c.course_name', 

          'c.course_academic_year_id', 'c.course_academic_semester_id', 

          'c.course_faculty_id', 'c.course_department_id', 'c.course_programme_id', 'c.course_unit',

          '', 'i.instructor_id', 'count(course_name) AS count'])

      ->from('courses c') 

      ->join('JOIN', 'academic_semesters cs', 'c.course_academic_semester_id = cs.academic_semester_id')

      ->join('JOIN', 'course_instructors ci', ' = ci.course_id')

      ->join('JOIN', 'instructors i', 'ci.instructor_id = i.instructor_id')


         'c.is_status' => '0',

         'i.user_id' => $user


      ->groupBy('i.instructor_id', 'c.course_name')

      ->orderBy(['i.instructor_id'=>SORT_DESC, 'c.course_name'=>SORT_DESC])


        $dataProvider = new ActiveDataProvider([

            'query' => $query, 'sort'=> ['defaultOrder' => ['id'=>SORT_DESC]],



        if (!$this->validate()) {

            // uncomment the following line if you do not want to any records when validation fails

            // $query->where('0=1');

            return $dataProvider;



            'id' => $this->id,

            'course_academic_year_id' => $this->course_academic_year_id,

            'course_academic_semester_id' => $this->course_academic_semester_id,

            'course_faculty_id' => $this->course_faculty_id,

            'course_department_id' => $this->course_department_id,

            'course_programme_id' => $this->course_programme_id,

            'course_unit' => $this->course_unit,


        $query->andFilterWhere(['like', 'course_type', $this->course_type])

            ->andFilterWhere(['like', 'course_name', $this->course_name])

            ->andFilterWhere(['like', 'course_code', $this->course_code]);

        return $dataProvider;         




    public function actionAvailableCoursesGroupings()


        $user = Yii::$app->getid->getId();

        $instructorSession = Yii::$app->session->get('instructor_id');        

        $searchModel = new CoursesSearch();

        $dataProvider = $searchModel->searchcoursegroupings(Yii::$app->request->queryParams, $user);

        return $this->render('available-courses-groupings', [

            'searchModel' => $searchModel,

            'dataProvider' => $dataProvider,



If I comment the andFilterWhere, the error becomes

The "query" property must be an instance of a class that implements the QueryInterface e.g. yii\db\Query or its subclasses.



How do I resolve thiese errors.


At the end of $query definition, you call ->all() method, so you will not have an ActiveQuery instance but an ActiveRecord arrays.

Remove ->all() at the end of $query definition to continue adding conditions.

And maybe I’d suggest these things:

  • using ActiveRecord instead of “hardcoding” the query via \yii\db\Query()
  • [size=2]using cameCase in method names[/size]
  • [size=2]get ID of logged user using WebUser[/size]