Esempio n. 1
0
 /**
  * Updates an existing Meal model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param string $id
  * @return mixed
  */
 public function actionUpdate($id)
 {
     $model = $this->findModel($id);
     $productImages = MealImage::findOne(['meal_id' => $id]);
     $getItems = MealItem::find()->all();
     foreach ($getItems as $key => $value) {
         $data[$value->id] = $value->name;
     }
     $getMealItems = MealItemsManage::find()->select(['item_id'])->where(['meal_id' => $id])->asArray()->all();
     $selected = [];
     if ($getMealItems) {
         $count = 0;
         foreach ($getMealItems as $key => $value) {
             $itemInArray[] = $value['item_id'];
             $count++;
         }
         $selected = $itemInArray;
     }
     //$selectedFlip = array_flip($selected);
     if ($model->load(Yii::$app->request->post())) {
         $dateTime = $model->deal_closing_time;
         //$model->day = $model->day;
         $model->deal_closing_time = strtotime($dateTime);
         $model->created_at = time();
         //$model->deal_status;
         if ($model->save()) {
             if (!empty($productImages)) {
                 //$productImages->meal_id = $model->id;
                 $this->uploadImage($productImages);
             } else {
             }
             $mealItems = Yii::$app->request->post('meal_id');
             if (!empty($mealItems) && !empty($itemInArray)) {
                 $diffItemsNew = array_diff($mealItems, $itemInArray);
                 $diffItemsDeleted = array_diff($itemInArray, $mealItems);
             } elseif (!empty($mealItems)) {
                 foreach ($mealItems as $key => $itemID) {
                     $mealItemsManage = new MealItemsManage();
                     $mealItemsManage->meal_id = $model->id;
                     $mealItemsManage->item_id = $itemID;
                     $mealItemsManage->created_at = time();
                     if ($mealItemsManage->save()) {
                     }
                 }
             }
             if (!empty($diffItemsNew)) {
                 foreach ($diffItemsNew as $key => $itemID) {
                     $mealItemsManage = new MealItemsManage();
                     $mealItemsManage->meal_id = $model->id;
                     $mealItemsManage->item_id = $itemID;
                     $mealItemsManage->created_at = time();
                     if ($mealItemsManage->save()) {
                     }
                 }
             }
             if (!empty($diffItemsDeleted)) {
                 foreach ($diffItemsDeleted as $key => $itemID) {
                     $mealItemsManage = MealItemsManage::findOne(['meal_id' => $id, 'item_id' => $itemID]);
                     if ($mealItemsManage->delete()) {
                     }
                 }
             }
             return $this->redirect(['view', 'id' => $model->id]);
         }
     } else {
         $model->deal_closing_time = \Yii::$app->formatter->asTime($model->deal_closing_time, "php:Y-m-d H:i:s");
         # 2014-10-03 14:09:20
         return $this->render('update', ['model' => $model, 'productImages' => $productImages, 'data' => $data, 'selected' => $selected]);
     }
 }
Esempio n. 2
0
 /**
  * Finds the MealItem model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param string $id
  * @return MealItem the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = MealItem::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }