/**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ArrayDataProvider
  */
 public function search($params = [])
 {
     $caloriesQuery = RecipeRepository::getCaloriesQuery('p.recipe_id')->createCommand()->sql;
     $proteinsQuery = RecipeRepository::getProteinsQuery('p.recipe_id')->createCommand()->sql;
     $fatsQuery = RecipeRepository::getFatsQuery('p.recipe_id')->createCommand()->sql;
     $carbohydratesQuery = RecipeRepository::getCarbohydratesQuery('p.recipe_id')->createCommand()->sql;
     $query = IngredientEntity::find()->select(['(\'' . IngredientEntity::TYPE_COUNT . '\') AS type', 'p.id', 'p.name', 'dp.count', "({$caloriesQuery}) * p.weight * dp.count AS calories", "({$proteinsQuery}) * `p`.`weight` AS `protein`", "({$fatsQuery}) * `p`.`weight` AS `fat`", "({$carbohydratesQuery}) * `p`.`weight` AS `carbohydrate`"])->from(['dp' => Diary::diary2portionsTableName()])->leftJoin(['p' => Portion::tableName()], 'p.id = dp.portion_id')->where(['dp.diary_id' => $this->id])->orderBy('calories DESC');
     $dataProvider = new ArrayDataProvider(['allModels' => $query->all(), 'sort' => ['attributes' => ['name', 'count', 'calories', 'protein', 'fat', 'carbohydrate'], 'defaultOrder' => ['name' => SORT_ASC]], 'pagination' => false]);
     return $dataProvider;
 }
 /**
  * @param integer $id
  * @return Query
  */
 public static function getPortionsWeightQuery($id)
 {
     return (new Query())->select('SUM(`dp`.`count`*`p`.`weight`) AS `weight`')->from(Diary::diary2portionsTableName() . ' `dp`')->leftJoin(Portion::tableName() . ' `p`', '`p`.`id` = `dp`.`portion_id`')->where("`dp`.`diary_id` = {$id}");
 }
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getDiaries()
 {
     return $this->hasMany(Diary::className(), ['id' => 'diary_id'])->viaTable(Diary::diary2portionsTableName(), ['portion_id' => 'id']);
 }