Advertisement
Guest User

Untitled

a guest
Jul 10th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. class AnswerController extends FOSRestController
  2. {
  3.  
  4. private $data;
  5.  
  6. /**
  7. * @param mixed $data
  8. */
  9. public function setData($data): void
  10. {
  11. $this->data = $data;
  12. }
  13.  
  14. /**
  15. * @Rest\Post("/answer")
  16. */
  17. public function postAnswerAction(Request $request)
  18. {
  19. if ($request->request->count() > 0 && $request->files->count() > 0)
  20. $this->setData(array_replace_recursive(
  21. $request->request->all(),
  22. $request->files->all()
  23. ));
  24. else
  25. $this->setData($request->request->all() ?: $request->files->all());
  26.  
  27. $batchSize = 20;
  28. $i = 1; foreach($this->getData() as $item) { ++$i;
  29. $serializer = SerializerBuilder::create()
  30. ->configureHandlers(function (HandlerRegistry $registry) {
  31. $registry->registerSubscribingHandler(new UserHandler($this->entityManager));
  32. $registry->registerSubscribingHandler(new OptionsHandler($this->entityManager));
  33. $registry->registerSubscribingHandler(new QuestionHandler($this->entityManager));
  34. })
  35. ->addDefaultHandlers()
  36. ->build();
  37. $object = $serializer->deserialize(json_encode($item), Answer::class, 'json');
  38. $this->entityManager->persist($object);
  39.  
  40. if (($i % $batchSize) === 0) {
  41. $this->entityManager->flush();
  42. $this->entityManager->clear();
  43. }
  44. }
  45. $this->entityManager->flush();
  46. $this->entityManager->clear();
  47.  
  48. return $this->handleView($this->view($this->getData(), Response::HTTP_CREATED, ['Access-Control-Allow-Origin' => '*']));
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement