leftJoin('fields_value_new','fields_value_new.item_id=balance.id AND fields_value_new.item_type=3'); // add conditions that should always apply here $dataProvider = new ActiveDataProvider([ 'query' => $query, ]); $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, 'type' => $this->type, //'summ' => $this->summ, 'dt_add' => $this->dt_add, ]); $query->andFilterWhere(['>=','dt_add', strtotime($this->dt_from) ?: null]); $query->andFilterWhere(['<=','dt_add', strtotime($this->dt_to) ?: null]); $query->andFilterWhere(['between', 'summ', $this->summ_from ?: 0, $this->summ_to ?: 9999999999]); $query->andFilterWhere(['fields_value_new.field_id'=>$this->field_name]); $query->andFilterWhere(['LIKE', 'fields_value_new.value', $this->field_value]); $query->orderBy('balance.dt_add DESC'); return $dataProvider; } public function getSummInfo() { $query = Balance::find() ->andFilterWhere(['>=','dt_add', strtotime($this->dt_from) ?: null]) ->andFilterWhere(['<=','dt_add', strtotime($this->dt_to) ?: null]) ->all(); $active_summ = 0; $passive_summ = 0; $difference = 0; foreach ($query as $item) { if($item->type == 1) { $active_summ += $item->summ; } else { $passive_summ += $item->summ; } } $difference = $active_summ - $passive_summ; return compact('active_summ', 'passive_summ', 'difference'); } }