/** * @param array $params * @param bool $filterByUser * @return \yii\db\Query */ public static function getQuery($params = [], $filterByUser = true) { $portionCaloriesSql = self::getPortionCaloriesQuery('`d`.`id`')->createCommand()->sql; $recipeCaloriesSql = self::getRecipeCaloriesQuery('`d`.`id`')->createCommand()->sql; $productCaloriesSql = self::getProductCaloriesQuery('`d`.`id`')->createCommand()->sql; $portWeightSql = self::getPortionsWeightQuery('`d`.`id`')->createCommand()->sql; $recWeightSql = self::getRecipesWeightQuery('`d`.`id`')->createCommand()->sql; $prodWeightSql = self::getProductsWeightQuery('`d`.`id`')->createCommand()->sql; $query = Diary::find()->select(['`d`.*', '`u`.`weighing_day` AS `weighingDay`', "COALESCE(({$portionCaloriesSql}), 0) + COALESCE(({$recipeCaloriesSql}), 0) + COALESCE(({$productCaloriesSql}), 0) AS `calories`", "COALESCE(({$prodWeightSql}), 0) + COALESCE(({$recWeightSql}), 0) + COALESCE(({$portWeightSql}), 0) AS `weight`"])->from(self::tableName() . ' `d`')->leftJoin(User::tableName() . ' `u`', '`u`.`id` = `d`.`user_id`'); if ($filterByUser) { $query->where(['`d`.`user_id`' => Yii::$app->user->id]); } return $query; }
public function getDelete($id) { if (Auth::user()->role == 'admin') { if (Diary::find($id)->delete()) { return redirect()->back()->with('msg', 'deleted'); } } if (Diary::where('user_id', Auth::user()->id)->where('id', $id)->exists()) { if (Diary::find($id)->delete()) { return redirect()->back()->with('msg', 'deleted'); } } return redirect()->back(); }
/** * Lists all Diary models. * @return mixed */ public function actionIndex() { $dataProvider = new ActiveDataProvider(['query' => Diary::find()]); return $this->render('index', ['dataProvider' => $dataProvider]); }
/** * Lists all Diary models. * @return mixed */ public function actionIndex() { $dataProvider = new ActiveDataProvider(['query' => Diary::find()]); $solidFood = DctSolidFood::find()->select('`dct_solid_food`.*, `dct_solid_food_loc`.`text` as text')->leftJoin('dct_solid_food_loc', '`dct_solid_food_loc`.`dct_solid_food_id` = `dct_solid_food`.`dct_solid_food_id`')->where(['dct_solid_food_loc.dct_language_id' => $this->currentLngId])->with('dctSolidFoodLocs')->asArray()->all(); return $this->render('index', ['dataProvider' => $dataProvider, 'solidFood' => $solidFood]); }