select([ 'cl.*', 'search_customer_count' => $countCustomerExp, ]); $query->alias('cl'); $query->joinWith('customers c'); $query->groupBy('cl.id'); // add conditions that should always apply here $dataProvider = new ActiveDataProvider([ 'query' => $query, 'sort' => [ 'attributes' => [ 'company_name', 'website', 'updated_at', 'search_customer_count' => [ 'asc' => [(string)$countCustomerExp => SORT_ASC], 'desc' => [(string)$countCustomerExp => SORT_DESC], ] ], 'defaultOrder' => [ 'company_name' => SORT_ASC, 'search_customer_count' => SORT_DESC ] ] ]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } // grid filtering conditions $query->andFilterWhere([ 'id' => $this->id, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at, ]); $query->andFilterWhere(['like', 'company_name', $this->company_name]) ->andFilterHaving([(string)$countCustomerExp => $this->search_customer_count]) ->andFilterWhere(['like', 'website', $this->website]) ->andFilterWhere(['like', 'notes', $this->notes]); return $dataProvider; } }