Symfony2 - Set a selected value for the entity field

前端 未结 4 685
忘了有多久
忘了有多久 2020-12-20 14:59

I\'m trying to set a selected value inside an entity field. In accordance with many discussions I\'ve seen about this topic, I tried to set the data option but

4条回答
  •  余生分开走
    2020-12-20 15:28

    You only need set the data of your field:

        
        class EventController extends Controller
        {
            // ...
    
            public function addAction()
            {
               $request = $this->getRequest();
                $em = $this->getDoctrine()->getManager();
    
                $event = new Event();
                $form = $this->createForm(new EventType(), $event);
    
                // -------------------------------------------
                // Suppose you have a place entity..
                $form->get('place')->setData($place);
                // That's all..
                // -------------------------------------------
    
                $formHandler = new EventHandler($form, $request, $em);
    
                if($formHandler->process()) {
                    $this->get('session')->getFlashBag()->add('success', "L'évènement a bien été ajouté.");
                    return $this->redirect($this->generateUrl('photo_event_list'));
                }
    
                return $this->render('RoyalMovePhotoBundle:Event:add.html.twig', array(
                    'form' => $form->createView()
                ));
            }
        }
        

提交回复
热议问题