/**
  * 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;
 }
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $this->addParam('categoryId', $params);
     $caloriesSql = self::getCaloriesQuery('`r`.`id`')->createCommand()->sql;
     $proteinsSql = self::getProteinsQuery('`r`.`id`')->createCommand()->sql;
     $fatsSql = self::getFatsQuery('`r`.`id`')->createCommand()->sql;
     $carbohydratesSql = self::getCarbohydratesQuery('`r`.`id`')->createCommand()->sql;
     $query = Recipe::find()->select(['r.*', 'categoryName' => 'c.name', 'calories' => "({$caloriesSql})", 'proteins' => "({$proteinsSql})", 'fats' => "({$fatsSql})", 'carbohydrates' => "({$carbohydratesSql})"])->from(self::tableName() . ' `r`')->leftJoin(RecipeCategory::tableName() . ' `c`', '`c`.`id` = `r`.`category_id`');
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     if (!$this->validate()) {
         return $dataProvider;
     }
     $query->andFilterWhere([]);
     $query->andFilterWhere(['like', '`r`.`name`', $this->name])->andFilterWhere(['like', '`r`.`description`', $this->description])->andFilterWhere(['like', '`c`.`name`', $this->categoryName])->andFilterWhere(['category_id' => $this->categoryId]);
     $dataProvider->sort = ['attributes' => ['name', 'categoryName', 'calories', 'proteins', 'fats', 'carbohydrates']];
     return $dataProvider;
 }
 /**
  * @param array|string|integer $params
  * @return Portion
  */
 public static function searchOne($params)
 {
     $where = is_array($params) ? $params : ['p.id' => $params];
     return Portion::find()->select(['p.*', 'p.weight', 'categoryName' => 'c.name', 'recipeName' => 'r.name', 'calories' => 'SUM(`rp`.`weight`*`prod`.`calories`)/SUM(`rp`.`weight`)*`p`.`weight`', 'proteins' => 'SUM(`rp`.`weight`*`prod`.`protein`)/SUM(`rp`.`weight`)*`p`.`weight`', 'fats' => 'SUM(`rp`.`weight`*`prod`.`fat`)/SUM(`rp`.`weight`)*`p`.`weight`', 'carbohydrates' => 'SUM(`rp`.`weight`*`prod`.`carbohydrate`)/SUM(`rp`.`weight`)*`p`.`weight`'])->from(['p' => Portion::tableName()])->innerJoin(['r' => Recipe::tableName()], '`r`.`id` = `p`.`recipe_id`')->innerJoin(['rp' => Recipe::recipe2productsTableName()], '`rp`.`recipe_id` = `r`.`id`')->leftJoin(['c' => RecipeCategory::tableName()], '`c`.`id` = `r`.`category_id`')->leftJoin(['prod' => Product::tableName()], '`prod`.`id` = `rp`.`product_id`')->where($where)->one();
 }