Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [Symfony2][SF][SF2] Pole w formularzu, które stworzy obiekt
Fluke
post
Post #1





Grupa: Zarejestrowani
Postów: 247
Pomógł: 9
Dołączył: 20.09.2010
Skąd: Kraków

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


Witam,

Mam pytanie odnośnie pojedynczego pola w formularzu, które po uzupełnieniu stworzy mi obiekt danego typu.
Mam encję Person z polami: [id, firstname, lastname, email, address]
Formularz:
  1. $builder->add(
  2. 'email',
  3. 'email',
  4. [
  5. 'property_path' => 'person.email',
  6. 'data_class' => Person::class,
  7. ]
  8. );

I w ten sposób chciałbym, żeby mi stworzyło encję typu Person z uzupełnionym polem email. Niestety dostaję: Expected argument of type "object or array", "NULL" given.

Jest sposób na to aby automatycznie w Symfony2 stworzył mi taką encję ? Zaznaczę, że próbowałem jeszcze z "empty_data" który zwracał obiekt Person ale też nie działa. Też chciałbym nie używać DataTransformer.

Pozdrawiam.
Go to the top of the page
+Quote Post
destroyerr
post
Post #2





Grupa: Zarejestrowani
Postów: 879
Pomógł: 189
Dołączył: 14.06.2006
Skąd: Bytom

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


Nie wiem o co chodzi. Dodajesz pole email i chcesz żeby było obiektem klasy Person? Jeżeli chcesz żeby formularz tworzył Ci obiekt to musisz ustawić odpowiednią opcję.
Poza tym podajesz treść błędu, nie wiemy gdzie się pojawia, nie wiemy co wywołujesz, że pojawia się ten błąd.
Go to the top of the page
+Quote Post
Fluke
post
Post #3





Grupa: Zarejestrowani
Postów: 247
Pomógł: 9
Dołączył: 20.09.2010
Skąd: Kraków

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


@destroyerr
Właśnie tak. Tak żebym ewentualnie nie musiał tworzyć formularza typu PersonType z 1 polem email który ma $resolver(['data_class] => Person::class);
Go to the top of the page
+Quote Post
kpt_lucek
post
Post #4





Grupa: Zarejestrowani
Postów: 428
Pomógł: 77
Dołączył: 10.07.2011
Skąd: Warszawa

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


Poczytaj o transformerach,
  1.  
  2. class MojFormType extends AbstractForm {
  3.  
  4. /**
  5.   * @param FormBuilderInterface $builder
  6.   * @param array $options
  7.   */
  8. public function buildForm(FormBuilderInterface $builder, array $options)
  9. {
  10. $builder
  11. ->add('school', 'EntityInt', array(
  12. 'label' => "Label",
  13. 'class' => 'ABCBundle\Entity\SuperEntity',
  14. 'query' => '',
  15. 'attr' => array(
  16. 'class' => 'super_class',
  17. )
  18. ));
  19. }
  20. }

  1. // MojBundle\Form\FormTypes\EntityIntType
  2. // typ EntityInt - zdefiniowany przeze mnie
  3. public function buildForm(FormBuilderInterface $builder, array $options)
  4. {
  5. $transformer = new Transformer($this->em);
  6. $transformer->setClass($options['class']);
  7. $transformer->setProperty('id');
  8. $builder->add(
  9. $builder->create('id', 'integer', array(
  10. 'error_bubbling' => false,
  11. 'label' => 'labelka'
  12. 'attr' => array(
  13. 'class' => "superklasa",
  14. )
  15. ))
  16. ->addModelTransformer($transformer)
  17. );
  18. }

Transofmera niestety podać nie mogę. Jeżeli Ci to pomoże, to...
- Interface DataTransformerInterface
- setClass przyjmuje klasę jako parametr - Twoją encję która będzie transformowana obustronnie {property}->obiekt, obiekt->{property}
- setProperty pole, dokladniej nazwa kolumny - znajdziesz w encji (Doctrine ORM Entity)

W praktyce to transformery bardzo ułatwiają życie i nie widzę potrzeby wyszukiwania innego sposobu (IMG:style_emoticons/default/smile.gif)


Pozdrawiam

Ten post edytował kpt_lucek 26.10.2014, 02:30:36
Go to the top of the page
+Quote Post
Fluke
post
Post #5





Grupa: Zarejestrowani
Postów: 247
Pomógł: 9
Dołączył: 20.09.2010
Skąd: Kraków

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


Do tego zrobiłem sobie klasę:

  1. class CustomType extends AbstractType {
  2. /**
  3.   * @var string
  4.   */
  5. protected $name;
  6.  
  7. /**
  8.   * @var callable
  9.   */
  10. protected $callback;
  11.  
  12. /**
  13.   * @var array
  14.   */
  15. protected $defaults;
  16.  
  17. /**
  18.   * @param string $name
  19.   * @param callable $callback
  20.   * @param array $defaults
  21.   */
  22. function __construct($name, $callback, array $defaults = []) {
  23. $this->name = $name;
  24. $this->callback = $callback;
  25. $this->defaults = $defaults;
  26. }
  27.  
  28. /**
  29.   * @param FormBuilderInterface $builder
  30.   * @param array $options
  31.   */
  32. public function buildForm(FormBuilderInterface $builder, array $options) {
  33. call_user_func_array($this->callback, [$builder, $options]);
  34. }
  35.  
  36.  
  37. /**
  38.   * @param OptionsResolverInterface $resolver
  39.   */
  40. public function setDefaultOptions(OptionsResolverInterface $resolver) {
  41. $resolver->setDefaults($this->defaults);
  42. }
  43.  
  44. /**
  45.   * @return string The name of this type
  46.   */
  47. public function getName() {
  48. return $this->name;
  49. }
  50. }


I mogę teraz używać w ten sposób:
  1. new CustomType('my_custom_form', function ($builder, $options) {
  2. $builder->add('firstname', 'text);
  3. $builder->add('lastname', 'text');
  4. }, ['data_class' => User::class]);
Go to the top of the page
+Quote Post

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: 23.12.2025 - 19:27