/** * @return array */ public function getPortionItems() { if ($this->_portionItems !== null) { return $this->_portionItems; } $this->_portionItems = [0 => '---']; $query = Portion::find()->select(['`p`.`id`', '`p`.`name`'])->from(Portion::tableName() . ' `p`')->orderBy('`p`.`name`'); if (!empty($this->portionCategoryId)) { $query->leftJoin(Recipe::tableName() . ' `r`', '`r`.`id` = `p`.`recipe_id`')->where(['`r`.`category_id`' => $this->portionCategoryId]); } $items = $query->asArray()->all(); if (!empty($items)) { foreach ($items as $item) { $this->_portionItems[$item['id']] = $item['name']; } } return $this->_portionItems; }
/** * @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(); }