2016-07-03 54 views
0

爲什麼我不能在我的函數createFormBuilder中使用變量$ idType?我無法在createFormBuilder中使用變量

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $idType = $options['data']->getEvenement()->getType()->getId(); 

    $builder 
     ->add('depart',  DateTimeType::class, array(
      'widget' => 'single_text' 
     )) 
     ->add('inscription', TextType::class) 
     ->add('prixNl',  IntegerType::class) 
     ->add('prixL',  IntegerType::class) 
     ->add('resultat', TextType::class) 
     ->add('sousType',  EntityType::class, array(
      'class'  => 'CalendrierBundle:SousType', 
      'choice_label' => 'nom', 
      'multiple'  => false, 
      'query_builder' => function (EntityRepository $er) { 
       return $er->createQueryBuilder('u') 
        ->where('u.typeId = :type') 
        ->setParameter('type', $idType); 
      } 
     )) 
     ->add('save',  SubmitType::class) 
    ; 
} 

她不承認

感謝您的幫助

+0

我建議增加一點點的措詞來解釋你想達到的目的。 –

+0

你得到什麼錯誤? – nbryans

回答

2

您應該使用use ($idType)關鍵字來從父範圍爲closure繼承$ idType變量。

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $idType = $options['data']->getEvenement()->getType()->getId(); 

    $builder 
     ->add('depart',  DateTimeType::class, array(
      'widget' => 'single_text' 
     )) 
     ->add('inscription', TextType::class) 
     ->add('prixNl',  IntegerType::class) 
     ->add('prixL',  IntegerType::class) 
     ->add('resultat', TextType::class) 
     ->add('sousType',  EntityType::class, array(
      'class'  => 'CalendrierBundle:SousType', 
      'choice_label' => 'nom', 
      'multiple'  => false, 
      'query_builder' => function (EntityRepository $er) use ($idType) { 
       return $er->createQueryBuilder('u') 
        ->where('u.typeId = :type') 
        ->setParameter('type', $idType); 
      } 
     )) 
     ->add('save',  SubmitType::class) 
    ; 
}