/**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $this->addParam('categoryId', $params);
     $query = Product::find()->select(['`p`.*', '`pc`.`name` AS `categoryName`'])->from(Product::tableName() . '`p`')->leftJoin(ProductCategory::tableName() . ' `pc`', '`p`.`category_id` = `pc`.`id`');
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     if (!$this->validate()) {
         return $dataProvider;
     }
     if (!isset($params['sort'])) {
         $query->orderBy('`p`.`name`');
     }
     $query->andFilterWhere(['`p`.`calories`' => $this->calories, '`p`.`protein`' => $this->protein, '`p`.`fat`' => $this->fat, '`p`.`carbohydrate`' => $this->carbohydrate, '`p`.`category_id`' => $this->categoryId, '`pc`.`name`' => $this->categoryName]);
     $query->andFilterWhere(['like', '`p`.`name`', $this->name])->andFilterWhere(['like', '`pc`.`name`', $this->categoryName]);
     $dataProvider->sort = ['attributes' => ['name', 'calories', 'protein', 'fat', 'carbohydrate', 'categoryName' => [SORT_ASC => '`pc`.`name` ASC', SORT_DESC => '`pc`.`name` DESC']]];
     return $dataProvider;
 }
 /**
  * @param integer $id
  * @return Query
  */
 public static function getProductCaloriesQuery($id)
 {
     /**
      * SELECT
      *   SUM(`prod`.`calories`*`dp`.`weight`)
      * FROM `diary_products` `dp`
      * LEFT JOIN `product` `prod` ON `prod`.`id` = `dp`.`product_id`
      * WHERE `dp`.`diary_id` = `d`.`id`
      */
     return (new Query())->select('SUM(`prod`.`calories`*`dp`.`weight`) AS `productCalories`')->from(Diary::diary2productsTableName() . ' `dp`')->leftJoin(Product::tableName() . ' `prod`', '`prod`.`id` = `dp`.`product_id`')->where("`dp`.`diary_id` = {$id}");
 }
 /**
  * @param array $params
  * @return ArrayDataProvider
  */
 public function search($params = [])
 {
     $query = IngredientEntity::find()->select(['(\'' . IngredientEntity::TYPE_WEIGHT . '\') AS type', 'p.id', 'p.name', 'rp.weight', '`rp`.`weight`*`p`.`calories` AS `calories`', '`rp`.`weight`*`p`.`protein` AS `protein`', '`rp`.`weight`*`p`.`fat` AS `fat`', '`rp`.`weight`*`p`.`carbohydrate` AS `carbohydrate`'])->from(['rp' => Recipe::recipe2productsTableName()])->leftJoin(['p' => Product::tableName()], 'p.id = rp.product_id')->where(['rp.recipe_id' => $this->id])->orderBy('calories DESC');
     $dataProvider = new ArrayDataProvider(['allModels' => $query->all(), 'sort' => ['attributes' => ['name', 'weight', 'calories', 'protein', 'fat', 'carbohydrate'], 'defaultOrder' => ['name' => SORT_ASC]], 'pagination' => false]);
     return $dataProvider;
 }
 /**
  * @param integer $id
  * @return Query
  */
 public static function getCarbohydratesQuery($id)
 {
     return (new Query())->select('SUM(`rp`.`weight`*`p`.`carbohydrate`)/SUM(`rp`.`weight`) AS `carbohydrate`')->from(['rp' => self::recipe2productsTableName()])->leftJoin(Product::tableName() . ' `p`', '`p`.`id` = `rp`.`product_id`')->where("`rp`.`recipe_id` = {$id}");
 }
 /**
  * @return array
  */
 public function getIngredients()
 {
     if ($this->_ingredients !== null) {
         return $this->_ingredients;
     }
     $this->_ingredients = [];
     $items = (new Query())->select(['`p`.`id` AS `id`', '`p`.`name` AS `name`', '`rp`.`weight` AS `weight`', '`p`.`calories` AS `calories`'])->from(self::recipe2productsTableName() . ' `rp`')->leftJoin(Product::tableName() . ' `p`', '`p`.`id` = `rp`.`product_id`')->where(['`rp`.`recipe_id`' => $this->id])->all();
     if (!empty($items)) {
         $this->_ingredients = $items;
     }
     return $this->_ingredients;
 }
 /**
  * @param integer|string $id
  * @return Query
  */
 public static function getProductsCountQuery($id)
 {
     return (new Query())->select('COUNT(*) AS `productsCount`')->from(Product::tableName())->where("`category_id` = {$id}");
 }
 /**
  * @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();
 }