Guest User

Untitled

a guest
Feb 3rd, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.82 KB | None | 0 0
  1. public function createSurvey(Request $request) {
  2.     //check if user is authenticated
  3.     if($current_user = $this->getUser()) {
  4.         //check if user is admin
  5.         if($current_user->getRole() == 'ROLE_ADMIN') {
  6.             $survey = new Survey();
  7.             $form = $this->createForm(SurveyType::class, $survey);
  8.  
  9.             $form->handleRequest($request);
  10.  
  11.             if ($form->isSubmitted() && $form->isValid()) {
  12.                 $entityManager = $this->getDoctrine()->getManager();
  13.  
  14.                 //get the data from the form
  15.                 $survey = $form->getData();
  16.  
  17.                 //since each of the options needs to be linked to the survey, get all options
  18.                 //the code seems to fail here, because the array is empty
  19.                 $options = $survey->getOptions();
  20.  
  21.                 //because the survey already contains the options and it would not add them,
  22.                 //reset the array inside the survey entity
  23.                 $survey->resetOptions();
  24.  
  25.                 //for each of the options,
  26.                 foreach($options as $option) {
  27.  
  28.                     //first set the amount the option has been voted on to 0
  29.                     $option->setVotes(0);
  30.  
  31.                     //then add it to the survey entity again- the survey will also add itself to the inverse side this way
  32.                     $survey->addOption($option);
  33.  
  34.                     //and finally persist the option entity
  35.                     $entityManager->persist($option);
  36.                 }
  37.  
  38.                 //make sure the survey is unlocked so it can be voted on
  39.                 $survey->unlock();
  40.  
  41.                 //and persist the survey, and force the entity manager to save all the entities
  42.                 $entityManager->persist($survey);
  43.                 $entityManager->flush();
  44.  
  45.                 //then return to the home page
  46.                 return $this->redirectToRoute("app_blog_list");
  47.             }
  48.  
  49.             //render the form
  50.             return $this->render('blog/blog.survey.new.twig', [
  51.                 'form' => $form->createView(),
  52.             ]);
  53.         } else {
  54.             return $this->redirectToRoute("app_blog_list");
  55.         }
  56.     } else {
  57.     return $this->redirectToRoute("app_blog_list");
  58.     }
  59. }
Add Comment
Please, Sign In to add comment