public function actionDeleteall() { $role = UserRoleDetector::getUserRole(); Yii::$app->response->format = Response::FORMAT_JSON; if ($role != 3 && $role != 4) { echo json_encode(array('status' => 0, 'error_code' => Codes::$UNAUTHORIZED, 'errors' => StatusCodeMessage::$UNAUTHORIZED), JSON_PRETTY_PRINT); } else { $ids = json_decode($_REQUEST['ids']); $stringIds = implode(",", $ids); if (User::deleteAll('user_id IN (' . $stringIds . ')')) { echo json_encode(array('status' => 1, 'code' => 200, 'data' => 'deleted'), JSON_PRETTY_PRINT); } else { echo json_encode(array('status' => 0, 'error_code' => Codes::$BAD_REQUEST), JSON_PRETTY_PRINT); } } }
/** * Deleting all records that succeded where condition * * @throws \yii\db\Exception */ public function actionDelete() { $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 { $query = new Query(); $model = new OrderApi(); $where = null; //result to contain all records to delete if (isset($_REQUEST['order_id'])) { $where['order_id'] = $_REQUEST['order_id']; } if (isset($_REQUEST['user_id'])) { $where['user_id'] = $_REQUEST['user_id']; } if (isset($_REQUEST['restaurant_id'])) { $where['restaurant_id'] = $_REQUEST['restaurant_id']; } if (isset($_REQUEST['status'])) { $where['status'] = $_REQUEST['status']; } $result = $query->from('orders')->select(['order_id', 'status', 'user_id', 'user_info', 'restaurant_id', 'order_info'])->where($where)->createCommand()->queryAll(); //serialize that data that should be in json format $result = $model->serializeToJson($result); if ($result != null) { //show all records that will be deleted echo json_encode(array('status_code' => Codes::$OK, 'message' => StatusCodeMessage::$OK, 'data' => $result), JSON_PRETTY_PRINT); //delete that records $delete = $query->createCommand()->delete('orders', $where)->execute(); if ($delete == true) { echo json_encode(array('status_code' => Codes::$OK, 'message' => StatusCodeMessage::$OK), JSON_PRETTY_PRINT); } else { echo json_encode(array('status_code' => Codes::$INERNAL, 'message' => StatusCodeMessage::$INERNAL), JSON_PRETTY_PRINT); exit; } } else { echo json_encode(array('status_code' => CODES::$NOT_FOUND, 'message' => StatusCodeMessage::$NOT_FOUND), JSON_PRETTY_PRINT); } } }