2012-09-15 40 views
1

我一直堅持這幾天,並嘗試了所有我可以在網上找到的解決方案,但沒有運氣。嵌套窗體錯誤「傳遞到選擇字段的實體必須管理」

我有一個學生鏈接到許多轉移。每次轉賬都與Classe相關聯。

要使表單使用以下類:StudentType,TransferType。

StudentType表單類有一個TransferType類型的字段。以下代碼。

StudentType.php

<?php 
// src/PS/PSchoolBundle/Form/StudentType.php 

namespace PS\PSchoolBundle\Form; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilder; 
use PS\PSchoolBundle\Entity\Responsible; 
use PS\PSchoolBundle\Entity\AuthorizedPerson; 
use PS\PSchoolBundle\Entity\Transfer; 

class StudentType extends AbstractType 
{ 
    public function buildForm(FormBuilder $builder, array $options) 
    { 
     $builder 
      ->add('subNumber', 'text', array('required' => false)) 
      ->add('subDate', 'date', array(
         'required' => false, 
         'widget' => 'single_text', 
         'format' => 'dd/MM/yyyy', 
         'attr' => array('class' => 'datepicker') 
        )) 
      ->add('lastName', 'text') 
      ->add('firstName', 'text') 
      ->add('lastNameSecondLanguage', 'text', array('required' => false)) 
      ->add('firstNameSecondLanguage', 'text', array('required' => false)) 
      ->add('birthDate', 'date', array('required' => false, 
         'widget' => 'single_text', 
         'format' => 'dd/MM/yyyy', 
         'attr' => array('class' => 'datepicker') 
        )) 
      ->add('birthPlace', 'text', array('required' => false)) 
      ->add('birthPlaceSecondLanguage', 'text', array('required' => false)) 
      ->add('gender', 'choice', array('choices' => array('' => 'Veuillez choisir', 'm' => 'Garçon', 'f' => 'Fille'))) 
      ->add('nationality', 'text', array('required' => false)) 
      ->add('homeLanguage', 'text', array('required' => false)) 
      ->add('bloodType', 'text', array('required' => false)) 
      ->add('familySituation', 'choice', array('required' => false, 
                  'choices' => array(
                  '' => 'Veuillez choisir', 
                  'm' => 'Mariés', 
                  'd' => 'Divorcés', 
                  'w' => 'Veuve', 
                  'a' => 'Autre'))) 
      ->add('schoolStatusBefore', 'choice', array('required' => false, 'choices' => array('' => 'Veuillez choisir', 's' => 'Scolarisé', 'ns' => 'Non scolarisé'))) 
      ->add('remark', 'textarea', array('required' => false)) 
      ->add('responsibles', 'collection', array('type' => new ResponsibleType, 
                 'prototype' => true, 
                 'allow_add' => true, 
                 'allow_delete' => true)) 
      ->add('authorizedPersons', 'collection', array('type' => new AuthorizedPersonType, 
                 'prototype' => true, 
                 'allow_add' => true, 
                 'allow_delete' => true)) 

      ->add('transfers', 'collection', array('type' => new TransferType, 
                 'prototype' => true, 
                 'allow_add' => true, 
                 'allow_delete' => true)); 
    } 

    public function getName() 
    { 
     return 'student'; 
    } 

    public function getDefaultOptions(array $options) 
    { 
     return array(
      'data_class' => 'PS\PSchoolBundle\Entity\Student' 
     ); 
    } 
} 

TransferType.php

<?php 
// src/PS/PSchoolBundle/Form/TransferType.php 

namespace PS\PSchoolBundle\Form; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilder; 
use PS\PSchoolBundle\Entity\Transfer; 
use Doctrine\ORM\EntityRepository; 

class TransferType extends AbstractType 
{ 

    public function buildForm(FormBuilder $builder, array $options) 
    { 
     $builder 
      ->add('reason', 'choice', array(
         'choices' => array('success' => 'Réussite', 'failure' => 'Échec'), 
         'required' => false 
         )) 
      ->add('date', 'date', array(
         'widget' => 'single_text', 
         'format' => 'dd/MM/yyyy', 
         'attr' => array('class' => 'datepicker') 
        )) 
      ->add('school', 'text', array('required' => false)) 
      ->add('classe', 'entity', array('class' => 'PSPSchoolBundle:Classe', 'property' => 'name')); 
    } 

    public function getName() 
    { 
     return 'transfer'; 
    } 

    public function getDefaultOptions(array $options) 
    { 
     return array(
      'data_class' => 'PS\PSchoolBundle\Entity\Transfer' 
     ); 
    } 
} 

當我添加了 「CLASSE」 領域所使用的TransferType,而不是顯示的形式,我得到這個錯誤拋出:

傳遞給選擇字段的實體必須管理500內部 服務器錯誤 - FormException

請注意,我已經嘗試過TransferType並且窗體顯示正確,類填充選項字段已填充。這個問題只出現在我一起使用TransferType和StudentType的時候。

感謝

編輯 - 下面是控制器的產生形式的行動:

public function byIdEditAction($id) 
{ 
     $em = $this->getDoctrine()->getEntityManager(); 

     if(!$student = $em->getRepository('PSPSchoolBundle:Student')->find($id)) 
     { 
     throw $this->createNotFoundException('Student[id='.$id.'] does not exist.'); 
     } 

     $form  = $this->createForm(new StudentType(), $student); 
     $formHandler = new StudentHandler($form, $this->get('request'), $em); 

     if($formHandler->process()) 
     { 
      $this->get('session')->setFlash('wehappy', "L'élève a été mis à jour."); 
      return $this->redirect($this->generateUrl('_studentsbyidshow', array('id' => $student->getId()))); 
     } 

     return $this->render('PSPSchoolBundle:Student:edit.html.twig', array(
      'form' => $form->createView(), 
      'student' => $student 
    )); 
} 
+0

你可以發佈生成表單的控制器的代碼嗎? –

+0

剛剛添加了生成表單的操作。謝謝你的幫助。 –

+0

我看着你的代碼,一切都很好。這個錯誤似乎表明Classe對象不是由實體管理器管理的。我想這個班是由教義管理的。是嗎?不太確定還有什麼可能是錯誤的 –

回答

0

我的問題其實是有聯繫的事實,我使用的是具有實體領域的StudentType (轉移)本身有一個實體字段(Classe)。 鏈接到Student對象的傳輸對象沒有鏈接到它的Classe,因此我引發了該異常,抱怨Classe對象需要添加到Classes選項字段中而沒有進行管理。這意味着它必須由實體經理管理。換句話說,它必須來自數據庫。

有兩種方法可以解決這個問題: 顯而易見的是修復數據庫中丟失的鏈接,因爲該關係在Doctrine上設置爲不可空。 第二種方式是創建一個默認CLASSE對象,並從數據庫中拉出,然後將其分配給轉移,像這樣:使用StudentType實際創建表單之前

if(!$classe = $em->getRepository('PSPSchoolBundle:Classe')->find(-2)) 
    { 
     throw $this->createNotFoundException('Classe[id='.$id.'] does not exist.'); 
    } 

    $transfer->setClasse($classe); 

...

希望這有助於某人。

相關問題