public function actionCategory($categoryId) { $category = RecipeCategory::findOne($categoryId); if (empty($category)) { throw new NotFoundHttpException('The requested page does not exist.'); } $searchModel = new PortionRepository(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); return $this->render(['searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'categoryName' => $category->name]); }
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 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; }
/** * @return array */ public function getRecipeCategoriesItems() { return RecipeCategory::getItems(); }
/** * @return array */ public function getIngredientsCategoriesListItems() { return RecipeCategory::getItems(); }
/** * @return array */ public function getCategoryIdItems() { if ($this->_categoryIdItems !== null) { return $this->_categoryIdItems; } $this->_categoryIdItems = []; $categories = RecipeCategory::find()->orderBy('id')->asArray()->all(); if (!empty($categories)) { foreach ($categories as $category) { $this->_categoryIdItems[$category['id']] = $category['name']; } } return $this->_categoryIdItems; }
/** * Finds the RecipeCategory model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return RecipeCategory the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = RecipeCategory::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * @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(); }