2014-02-07 80 views
1

我使用Symfony2和樹枝作爲模板引擎。如何渲染模板中的選擇字段

在我的控制,我有以下形式:

 $usr= $this->get('security.context')->getToken()->getUser();  
     $associatedmissions = array($usr->getMissions()); 

     $form = $this->createFormBuilder($product)   
     ->add('mission', 'choice', array(
      'choices' => $associatedmission, 
      'multiple' => false, 
      'expanded' => true,)) 

但是當我打電話的頁面,就會出現錯誤:

Catchable Fatal Error: Object of class Doctrine\ORM\PersistentCollection could not be 
converted to string in 
C:\BitNami\wampstack-5.4.23- 
0\frameworks\symfony\vendor\symfony\symfony\src\Symfony\Component\Translation\ 
IdentityTranslator.php line 65 

在探查我可以看到錯誤:

CRITICAL - Uncaught PHP Exception Twig_Error_Runtime: "An exception has been thrown 
during the rendering of a template ("") in "form_div_layout.html.twig" at line 99." at 
C:\BitNami\wampstack-5.4.23-0\frameworks\symfony\app\cache\dev\classes.php line 4372 

Context: {"exception":"Object(Twig_Error_Runtime)"} 

發生了什麼事?

這很奇怪,因爲它工作正常

->add('mission', 'entity') 

-UPDATE--

這裏我的控制器:

public function createAction(Request $request) 
    {           
     $product = new Product(); 

     $usr= $this->get('security.context')->getToken()->getUser();  
     $associatedmissions = array($usr->getMissions()); 

      $form = $this->createFormBuilder($product)          
      ->add('name', 'text') 
      ->add('mission', 'choice', array(
       'choices' => $associatedmissions, 
       'multiple' => false, 
       'expanded' => false,)) 

      ->add('save', 'submit') 
      ->getForm();  


     $form->handleRequest($request); 
     if ($form->isValid()) { 

     $em = $this->getDoctrine()->getManager();  
     $em->persist($product); 
     $em->flush(); 

     return $this->render('AcmeGroundStationBundle:Product:tasksuccess.html.twig', array('product' => $product)); 
     }  

     return $this->render('AcmeGroundStationBundle:Product:formupload.html.twig', array(
     'form' => $form->createView() 
     )); 
} 
+0

你是如何渲染它在'枝'字段? –

+0

@Victor現在只用'{{form_start(form)}} {{form_end(form)}}' –

+0

您是否像'$ form-> createView()'那樣傳遞'form'變量來'返回'$ this - > render()方法? –

回答

1

a您有這個在您的控制器:

$usr= $this->get('security.context')->getToken()->getUser();  
$associatedmissions = array($usr->getMissions()); 

$usr->getMissions()是返回的ArrayCollection(基於你的實體關係)。這種方式$associatedmissions是一個包含一個元素的數組 - [1] =相關任務對象的ArrayCollection。

在我看來,你必須添加到您的用戶實體法:

public function getMissionsArray() 
{ 
    $missionArray = array(); 

    foreach ($this->missions as $key => $val) 
    { 
     $missionArray[$key] = $val->__toString(); 
    } 
    return $missionArray; 
} 

而在你createFormBuilder語句中使用數組作爲選擇PARAM。

當您驗證您的表單時,您必須在保留產品之前爲每個選擇創建任務對象。

$this->getDoctrine() 
      ->getRepository('AcmeGroundStationBundle:Mission') 
      ->find($product->mission); 

編輯

// User Entity 
// add this function - will return array of (id => mission.name,) 
public function getMissionsArray() 
{ 
    $missionArray = array(); 

    foreach ($this->missions as $key => $val) 
    { 
           // change this to whatever human readable getter 
     $missionArray[$key] = $val->__toString(); 
    } 
    return $missionArray; 
} 

// Controller  
public function createAction(Request $request) 
    {           
     $product = new Product(); 

     $usr= $this->get('security.context')->getToken()->getUser();  
     $associatedmissions = $usr->getMissionsArray(); 

      $form = $this->createFormBuilder($product)          
      ->add('name', 'text') 
      ->add('mission', 'choice', array(
       'choices' => $associatedmissions, 
       'multiple' => false, 
       'expanded' => false,)) 

      ->add('save', 'submit') 
      ->getForm();  


     $form->handleRequest($request); 
     if ($form->isValid()) { 

     // save selected array item as a Mission object related to Product 
     $product->setMission($this->getDoctrine() 
      // check if Mission Entity is placed in good bundle ;) 
      ->getRepository('AcmeGroundStationBundle:Mission') 
      ->find($form->get('mission')->getData())); 

     $em = $this->getDoctrine()->getManager();  
     $em->persist($product); 
     $em->flush(); 

     return $this->render('AcmeGroundStationBundle:Product:tasksuccess.html.twig', array('product' => $product)); 
     }  

     return $this->render('AcmeGroundStationBundle:Product:formupload.html.twig', array(
     'form' => $form->createView() 
     )); 
} 

祝您好運!

+0

一如既往地感謝您的準備和可用性。我不明白如何修改我的控制器。我也有錯誤:'不能訪問私人財產Acme \ GroundStationBundle \ Entity \ Product :: $任務' –

+0

更新。與: $ associatedmissions = $ this-> getDoctrine() - > getRepository('AcmeManagementBundle:Mission') - > findAll($ product-> mission); 我又一次得到了所有的使命,而不僅僅是那些相關的使命。 –

+0

檢查我的答案,我添加了完整的代碼 – WebHQ

0

嘗試使用重定向。它工作嗎?

if ($form->isValid()) { 

     $em = $this->getDoctrine()->getManager();  
     $em->persist($product); 
     $em->flush(); 

     return $this->redirect($this->generatePath('current_route_name')); 
    } 

    return $this->render('AcmeGroundStationBundle:Product:formupload.html.twig', array(
     'form' => $form->createView() 
    )); 

其中current_route_name被您當前頁面的路由名替換。

+0

沒有什麼變化。其實我也嘗試用重定向替換第二個'render',但是我獲得了'Error:調用未定義的方法:generatePath'使用'generateURL'我得到'La pagina web ha generato un loop di reindirizzamento'(有一個循環在重定向) –

+0

您是否在控制器類中擴展了Symfony Symfony \ Bundle \ FrameworkBundle \ Controller \ Controller? –

+0

是的,我喜歡!使用Symfony \ Bundle \ FrameworkBundle \ Controller \ Controller; –