Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [Symfony2][form] Jak dodać dynamicznie pole encji
Forum PHP.pl > Forum > PHP > Frameworki
silverwind
Moje encje Właściciel(Owner)jeden do wielu (Nieruchomość)jeden do wielu Wpłata(Donation)
Tworze formularz dodający nową wpłatę w formularzu są do wyboru dwie encje : Właściciel i Nieruchomość. Chcę aby po wyborze encji Właściciela był wybór tylko tych nieruchomości, które on posiada. Może mały przykład ktoś wrzuci. Jak to można rozwiązać. Dodałem do encji Property eventlistener. ale zwracami pustą tablicę.

  1. public function buildForm(FormBuilderInterface $builder, array $options)
  2. {
  3. $user = $options['user'];
  4.  
  5.  
  6.  
  7. $builder
  8. ->add('name', TextType::class,
  9. 'label' => 'Nazwa wpłaty'))
  10. ->add('price', MoneyType::class,
  11. 'label' => 'Cena',
  12. 'currency' => 'false'
  13. ))
  14. ->add('createDate', DateType::class,
  15. 'label' => 'Data',
  16. 'widget' => 'choice',
  17. 'years' => range(date('Y'), date('Y')-70)
  18. ))
  19. ->add('owner', EntityType::class,
  20. 'label' => 'Właściciel',
  21. 'class' => 'Property\ManagementBundle\Entity\Owner',
  22. 'choice_label' =>function($Owner) {
  23. return $Owner->getName().''.$Owner->getAdress();
  24. },
  25. 'required' => false,
  26.  
  27. 'empty_value' => 'Wybierz właściciela',
  28. 'query_builder' => function(OwnerRepository $o) use ($user) {
  29. return $o->createQueryBuilder('o')
  30. ->select('o,p')
  31. ->Join('o.property', 'p')
  32. ->where('o.user = :user')
  33.  
  34. ->setParameter('user', $user);
  35.  
  36. }
  37.  
  38. ));
  39. $builder->addEventListener(
  40. FormEvents::PRE_SET_DATA,
  41. function (FormEvent $event) {
  42. $form = $event->getForm();
  43.  
  44.  
  45. $data = $event->getData();
  46. dump($data);
  47. $owner = $data->getOwner();
  48. $property = null === $owner ? array() : $owner->getAvailableProperty();
  49.  
  50. $form->add('property', EntityType::class, array(
  51. 'class' => 'Property\ManagementBundle\Entity\Property',
  52. 'property' =>'adress',
  53. 'empty_value' => 'Wybierz nieruchomość',
  54. 'choices' => $property,
  55. ));
  56. }
  57. );
  58.  
  59.  
  60.  
  61. }
  62.  
  63. /**
  64.   * @param OptionsResolver $resolver
  65.   */
  66. public function configureOptions(OptionsResolver $resolver)
  67. {
  68. $resolver->setDefaults(array(
  69. 'data_class' => 'Property\ManagementBundle\Entity\Donation',
  70. 'user' => null
  71. ));
  72. }
destroyerr
Cytat
ale zwracami pustą tablicę.

Gdzie zwraca Ci pustą tablicę?
silverwind
przy wyborze encji nieruchomości
destroyerr
Nie ma to jak precyzja w definiowaniu problemu.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.