/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $recipesCountSql = self::getRecipesCountQuery('`rc`.`id`')->createCommand()->sql; $portionsCount = self::getPortionsCountQuery('`rc`.`id`')->createCommand()->sql; $query = RecipeCategory::find()->select(['`rc`.*', "({$recipesCountSql}) AS `recipesCount`", "({$portionsCount}) AS `portionsCount`"])->from(RecipeCategory::tableName() . ' `rc`'); $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; } $query->andFilterWhere([]); $query->andFilterWhere(['like', '`rc`.`name`', $this->name]); $dataProvider->sort = ['attributes' => ['name', 'recipesCount', 'portionsCount'], 'defaultOrder' => ['name' => SORT_ASC]]; return $dataProvider; }
/** * @return array */ public function getCategoryIdItems() { if ($this->_categoryIdItems !== null) { return $this->_categoryIdItems; } $this->_categoryIdItems = []; $categories = RecipeCategory::find()->orderBy('id')->asArray()->all(); if (!empty($categories)) { foreach ($categories as $category) { $this->_categoryIdItems[$category['id']] = $category['name']; } } return $this->_categoryIdItems; }