2017-08-27 51 views
1

[設置]集實體參數

  • Symfony的3
  • BoxEntity:[ID,名稱]
  • CandyEntity:[ID,名稱]

[問題]

目前,在創建新糖果時,我必須選擇一個盒子作爲父實體。
事情是,我希望這個選擇是自動化的。
該框已經在數據庫中註冊,並且會話持有當前框參數以方便查找。
但是我不知道如何在數據發佈後將其應用於糖果實體。

[FILES]

的appbundle /控制器/ CandyController.php

public function newAction(Request $request) { 
    $$candy= new Candy(); 
    $form = $this->createForm('AppBundle\Form\CandyType', $conference); 
    $form->handleRequest($request); 

    if ($form->isSubmitted() && $form->isValid()) { 
     $em = $this->getDoctrine()->getManager(); 
     $em->persist($candy); 
     $em->flush(); 

     return $this->redirectToRoute('candy_show', array('id' => $candy->getId())); 
    } 

    return $this->render('candy/new.html.twig', array(
     'candy' => $candy, 
     'form' => $form->createView(), 
    )); 
} 

的appbundle /形式/ CandyType.php

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder->add('nom') 
      ->add('box'); //Remove from form, and set manually 
} 

我沒有read this page,但可以」弄清楚如何正確地做到這一點。
如果有人會給我一個完整的例子來解決我的問題,這將非常感激。

+0

您是將它連接到數據庫中的現有盒子,還是正在創建一個新盒子,但是已經在會話中設置了參數?會話本身存儲了什麼? –

+0

@JasonRoman數據庫中已存在此框。會話保存箱數據(id,名稱)。編輯我的帖子以添加詳細信息...;) – Preciel

回答

1

你有多種選擇來執行你想要的。後您的表單提交,你可以設置的值:

public function newAction(Request $request) 
{ 
    $em = $this->getDoctrine()->getManager(); 

    $candy = new Candy(); 
    $box = $em->find('AppBundle\Entity\Box', $this->get('session')->get('boxId')); 

    $form = $this->createForm('AppBundle\Form\CandyType', $candy); 
    $form->handleRequest($request); 

    if ($form->isSubmitted() && $form->isValid()) { 
     // add the box entity to the candy 
     $candy->setBox($box); 

     $em->persist($candy); 
     $em->flush(); 

     return $this->redirectToRoute('candy_show', array('id' => $candy->getId())); 
    } 

    return $this->render('candy/new.html.twig', array(
     'candy' => $candy, 
     'form' => $form->createView(), 
    )); 
} 

你可以把它放在糖果實體將它傳遞給createForm()呼叫之前,雖然它可能不是對實體做形式handleRequest()調用後留:

$em = $this->getDoctrine()->getManager(); 

    $candy = new Candy(); 
    $box = $em->find('AppBundle\Entity\Box', $this->get('session')->get('boxId')); 

    $candy->setBox($box); 

    $form = $this->createForm('AppBundle\Form\CandyType', $candy); 
    $form->handleRequest($request); 

您可以在表單事件中以您嘗試的方式執行此操作。什麼,你會想要做的是注入實體管理器和會話到您的形式,並且把你的形式作爲一種服務:

public function CandyType extends AbstractType 
{ 
    private $em; 
    private $session; 

    public function __construct(EntityManager $em, SessionInterface $session) 
    { 
     $this->session = $session; 
     $this->em  = $em; 
    } 

    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     // ... build the form 

     $builder->addEventListener(
      FormEvents::PRE_SET_DATA, 
      function (FormEvent $event) { 
       $form = $event->getForm(); 

       $candy = $event->getData(); 
       $box = $this->em->find('AppBundle\Entity\Box', $this->session->get('boxId'); 

       $candy->setBox($box); 
      } 
     ); 
    } 
} 

您可能需要做的是在POST_SET_DATAPOST_SUBMIT事件代替,但我不當然。我還在控制器中使用了$this->get('session'),但取決於Symfony版本(> 3.3),您也可以將其作爲服務注入到控制器中。

無論哪種方式,主要概念是使用Doctrine從會話中使用存儲的Box ID在會話中獲取Box實體,然後在您的Candy實體上設置該實體。您甚至可以使用hidden字段來獲得相同的結果。正如我之前所說,有很多方法可以解決您的問題。

+0

您的第一個解決方案正在工作。雖然我有一個警告說「Excepted \ AppBundle \ Entity \ Box,有對象」。意思是缺少一點調整。 – Preciel

+0

這很奇怪 - 雖然沒有看到實體定義,但很難說出問題出在什麼地方......或者你得到了哪些錯誤。 –

+0

這裏有一個快速截圖(我將在稍後刪除)https://www.dropbox.com/s/5c912q07tmommk4/screenshot.png?dl=0 – Preciel