public function actionCategory($categoryId) { $category = RecipeCategory::findOne($categoryId); if (empty($category)) { throw new NotFoundHttpException('The requested page does not exist.'); } if ($this->isAjax()) { $model = new Portion(); $model->recipeCategoryId = $categoryId; return $this->renderPartial('@app/views/partials/_category-ingredients-dropdown-list', ['model' => $model, 'form' => ActiveForm::begin()]); } else { $searchModel = new RecipeRepository(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); return $this->render(['searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'categoryName' => $category->name]); } }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ArrayDataProvider */ public function search($params = []) { $caloriesQuery = RecipeRepository::getCaloriesQuery('dr.recipe_id')->createCommand()->sql; $proteinsQuery = RecipeRepository::getProteinsQuery('dr.recipe_id')->createCommand()->sql; $fatsQuery = RecipeRepository::getFatsQuery('dr.recipe_id')->createCommand()->sql; $carbohydratesQuery = RecipeRepository::getCarbohydratesQuery('dr.recipe_id')->createCommand()->sql; $query = IngredientEntity::find()->select(['(\'' . IngredientEntity::TYPE_WEIGHT . '\') AS type', 'r.id', 'r.name', 'dr.weight', "({$caloriesQuery}) * `dr`.`weight` AS `calories`", "({$proteinsQuery}) * `dr`.`weight` AS `protein`", "({$fatsQuery}) * `dr`.`weight` AS `fat`", "({$carbohydratesQuery}) * `dr`.`weight` AS `carbohydrate`"])->from(['dr' => Diary::diary2recipesTableName()])->leftJoin(['r' => Recipe::tableName()], 'r.id = dr.recipe_id')->where(['dr.diary_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; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $this->addParam('categoryId', $params); $caloriesSql = RecipeRepository::getCaloriesQuery('`p`.`recipe_id`')->createCommand()->sql; $proteinsSql = RecipeRepository::getProteinsQuery('`r`.`id`')->createCommand()->sql; $fatsSql = RecipeRepository::getFatsQuery('`r`.`id`')->createCommand()->sql; $carbohydratesSql = RecipeRepository::getCarbohydratesQuery('`r`.`id`')->createCommand()->sql; $query = Portion::find()->select(['`p`.*', 'calories' => "({$caloriesSql})*`p`.`weight`", 'proteins' => "({$proteinsSql})*`p`.`weight`", 'fats' => "({$fatsSql})*`p`.`weight`", 'carbohydrates' => "({$carbohydratesSql})*`p`.`weight`", 'categoryName' => 'c.name', 'recipeCategoryId' => 'r.category_id'])->from(self::tableName() . ' `p`')->leftJoin(Recipe::tableName() . ' `r`', '`r`.`id` = `p`.`recipe_id`')->leftJoin(RecipeCategory::tableName() . ' `c`', '`c`.`id` = `r`.`category_id`'); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { return $dataProvider; } $query->andFilterWhere(['`p`.`weight`' => $this->weight, '`r`.`category_id`' => $this->categoryId]); $query->andFilterWhere(['like', '`p`.`name`', $this->name])->andFilterWhere(['like', '`c`.`name`', $this->categoryName]); $dataProvider->sort = ['attributes' => ['name', 'categoryName', 'calories', 'weight', 'proteins', 'fats', 'carbohydrates']]; return $dataProvider; }
/** * @param integer $id * @return Recipe|bool */ public static function findById($id) { return RecipeRepository::searchOne($id); }