public function exportRestaurantMenuToCsv($id) { $restaurantName = Restaurant::findOne(['restaurant_id' => $id])->name; Yii::$app->params['uploadPath'] = Yii::$app->basePath . '/web/upload/'; $string = "name, description, price \n"; $product = null; foreach (Warehouse::find()->where(['restaurant_id' => $id])->batch(1) as $warehouse) { try { $product = Product::findOne(['product_id' => $warehouse[0]->product_id]); $string .= '"' . $product->name . '","' . $product->description . '","' . $warehouse[0]->price . '"'; $string .= "\n"; } catch (Exception $e) { } } $path = $this->saveStringToCsvFile($string, $restaurantName); $this->giveUserFile($path); }
/** * Finds the Restaurant model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param string $id * @return Restaurant the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Restaurant::findOne($id)) !== null) { $model->scenario = Restaurant::SCENARIO_READ; return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * Creating new order * User have to give restaurant_id and it should exist in table restaurants * Other attributes is optional, gives status value of Order::STATUS_CREATED * * @throws \yii\db\Exception */ public function actionCreate() { $role = UserRoleDetector::getUserRole(); Yii::$app->response->format = Response::FORMAT_JSON; if ($role != 3 && $role != 4) { echo json_encode(array('error_code' => Codes::$UNAUTHORIZED, 'errors' => StatusCodeMessage::$UNAUTHORIZED), JSON_PRETTY_PRINT); } else { $params = $_REQUEST; //checking restaurant_id if (!isset($params['restaurant_id'])) { echo json_encode(array('status_code' => CODES::$BAD_REQUEST, 'message' => StatusCodeMessage::$BAD_REQUEST), JSON_PRETTY_PRINT); exit; } else { $restaurant = Restaurant::findOne(['restaurant_id' => $params['restaurant_id']]); if ($restaurant == null) { echo json_encode(array('status_code' => CODES::$NOT_FOUND, 'message' => StatusCodeMessage::$NOT_FOUND), JSON_PRETTY_PRINT); exit; } } $query = new Query(); $result = $query->createCommand()->insert('orders', ["user_id" => isset($params['user_id']) ? User::findOne(['user_id' => $params['user_id']]) != null ? $params['user_id'] : null : null, "user_info" => isset($params['user_info']) ? $params['user_info'] : null, "restaurant_id" => $params['restaurant_id'], "order_info" => isset($params['order_info']) ? $params['order_info'] : '', "status" => Order::STATUS_CREATED, "other_users" => ''])->execute(); if ($result == null) { echo json_encode(array('status_code' => Codes::$INERNAL, 'message' => StatusCodeMessage::$INERNAL), JSON_PRETTY_PRINT); exit; } else { $model = $query->from('orders')->select(['order_id', 'user_id', 'user_info', 'restaurant_id', 'order_info'])->where(['order_id' => $query->max('order_id')])->createCommand()->queryAll(); echo json_encode(array('status_code' => Codes::$CREATED, 'message' => StatusCodeMessage::$CREATED, 'data' => $model), JSON_PRETTY_PRINT); } } }