Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function createSurvey(Request $request) {
- //check if user is authenticated
- if($current_user = $this->getUser()) {
- //check if user is admin
- if($current_user->getRole() == 'ROLE_ADMIN') {
- $survey = new Survey();
- $form = $this->createForm(SurveyType::class, $survey);
- $form->handleRequest($request);
- if ($form->isSubmitted() && $form->isValid()) {
- $entityManager = $this->getDoctrine()->getManager();
- //get the data from the form
- $survey = $form->getData();
- //since each of the options needs to be linked to the survey, get all options
- //the code seems to fail here, because the array is empty
- $options = $survey->getOptions();
- //because the survey already contains the options and it would not add them,
- //reset the array inside the survey entity
- $survey->resetOptions();
- //for each of the options,
- foreach($options as $option) {
- //first set the amount the option has been voted on to 0
- $option->setVotes(0);
- //then add it to the survey entity again- the survey will also add itself to the inverse side this way
- $survey->addOption($option);
- //and finally persist the option entity
- $entityManager->persist($option);
- }
- //make sure the survey is unlocked so it can be voted on
- $survey->unlock();
- //and persist the survey, and force the entity manager to save all the entities
- $entityManager->persist($survey);
- $entityManager->flush();
- //then return to the home page
- return $this->redirectToRoute("app_blog_list");
- }
- //render the form
- return $this->render('blog/blog.survey.new.twig', [
- 'form' => $form->createView(),
- ]);
- } else {
- return $this->redirectToRoute("app_blog_list");
- }
- } else {
- return $this->redirectToRoute("app_blog_list");
- }
- }
Add Comment
Please, Sign In to add comment