/** * 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; }
/** * @param integer $id * @return Query */ public static function getRecipesWeightQuery($id) { return (new Query())->select('SUM(`dr`.`weight`) AS `weight`')->from(Diary::diary2recipesTableName() . ' `dr`')->where("`dr`.`diary_id` = {$id}"); }
/** * @return \yii\db\ActiveQuery */ public function getDiary() { return $this->hasMany(Diary::className(), ['id' => 'calculating_id'])->viaTable(Diary::diary2recipesTableName(), ['recipe_id' => 'id']); }