/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = UserReadAlerts::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['User_id' => $this->User_id, 'WeatherAlert_id' => $this->WeatherAlert_id, 'isRead' => $this->isRead]); return $dataProvider; }
public function getUnreadAlertsCount($type) { $query = new Query(); $ids = []; $timePeriodForAlert = $type == 0 ? Yii::$app->params['timePeriodForRecentPreAlerts'] : Yii::$app->params['timePeriodForRecentPostAlerts']; $allAlerts = WeatherAlert::find()->where(['type' => $type, 'status' => WeatherAlert::STATUS_ACTUAL])->andFilterWhere(['>', 'date', time() - $timePeriodForAlert * 3600])->all(); foreach ($allAlerts as $alert) { $ids[] = $alert->id; } // var_dump($ids);die; // $readedAlerts = UserReadAlerts::find()->andFilterWhere(['in','WeatherAlert_id',implode(',',$ids)])->all(); $readedAlerts = UserReadAlerts::find()->where(['WeatherAlert_id' => $ids, 'User_id' => Yii::$app->user->id])->count(); return count($ids) - (int) $readedAlerts; }