Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [Symfony2][form] Jak dodać dynamicznie pole encji
silverwind
post
Post #1





Grupa: Zarejestrowani
Postów: 80
Pomógł: 0
Dołączył: 8.02.2013

Ostrzeżenie: (0%)
-----


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. }
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 25.08.2025 - 04:40