2013-04-13 72 views
1

我要收回語美洲實體InscritSeminaireType:Symfony2的 - 實體表單類型query_builder

class InscritSeminType extends AbstractType { 

    public function buildForm(FormBuilderInterface $builder, array $options) { 

     $builder 
      //->add('dateInscrit') 
      ->add('seminaire','entity', array(
       'class' => 'EspritSeminaireBundle:Seminaire', 
       'query_builder' => function($repository)use ($id) { 
        return $repository->getSeminaireLibre('id') 
         ->setParameter('id', $id) 
        ; 
       },       
       'required' => false,    
      )) 
     //->add('etudiant') 
     ; 
    } 

    public function setDefaultOptions(OptionsResolverInterface $resolver) { 
     $resolver->setDefaults(array(
      'data_class' => 'Esprit\SeminaireBundle\Entity\InscritSemin', 

     )); 
    } 

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

但它返回一個錯誤:

Notice: Undefined variable: id in C:\wamp\www\PFE\EOL\src\Esprit\SeminaireBundle\Form\InscritSeminType.php line 22

所以可能有人告訴我,我怎麼能解決這個問題?

+0

哪一行是「第22行」? – luiges90

+0

實際上,在查詢構建器的回調之前,Id沒有定義。 – gremo

回答

1

如果我理解正確,您想從表單構建器的id中恢復出精確的「Seminaire」,那麼必須先將該ID傳遞給該類。

下面是一個如何去做的例子。你必須在兩個地方進行更改。

首先在控制類:

// NOTICE THE $id, you have to pass it to the constructor. 
$form = $this->createForm(new InscritSeminType($id), $entity); 
在表單類型

然後:

class InscritSeminType extends AbstractType { 

    private $id; 

    public function __construct($id){ 
     $this->id = $id // Keep the id of the item. 
    } 

    public function buildForm(FormBuilderInterface $builder, array $options) { 
     $id = $this->id; // Retrieve the id 

     $builder 
      ->add('seminaire','entity', array(
       'class' => 'EspritSeminaireBundle:Seminaire', 
       'property' => 'id', // To tell the mapping is on the id. 
       'query_builder' => function($repository) use ($id) { 
        return $repository->getSeminaireLibre($id) 
       },  
       'required' => false, 
       )) 
     ; 
    } 

    public function setDefaultOptions(OptionsResolverInterface $resolver) { 
     $resolver->setDefaults(array(
      'data_class' => 'Esprit\SeminaireBundle\Entity\InscritSemin', 

     )); 
    } 

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

你傳遞的ID在構造函數中,然後檢索它在buildForm功能。

相關問題