Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [Symfony][Symfony2][SF2]Formularz w formularzu, nie wiem dokładnie jak to nazwać
skowron-line
post
Post #1





Grupa: Zarejestrowani
Postów: 4 340
Pomógł: 542
Dołączył: 15.01.2006
Skąd: Olsztyn/Warszawa

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


Więc chce zrobić coś takiego
Mam 2 tabele
users i users_email_addresses
i teraz chce na stronie mieć 2 inputy
- nazwa użytkownika
- email użytkownika

I teraz tak stworzyłem sobie Entity
  1. class Users
  2. {
  3. /**
  4.   * @var integer
  5.   *
  6.   * @ORM\Column(name="id_users", type="smallint", nullable=false)
  7.   * @ORM\Id
  8.   * @ORM\GeneratedValue(strategy="IDENTITY")
  9.   */
  10. private $id;
  11.  
  12. /**
  13.   * @var string
  14.   *
  15.   * @ORM\Column(name="name", type="string", length=100, nullable=true)
  16.   */
  17. private $name;
  18. /**
  19.   *
  20.   * @var ArrayCollection
  21.   * @ORM\OneToMany(targetEntity="UsersEmailAddresses", mappedBy="users")
  22.   */
  23. private $email;

i
  1. class UsersEmailAddresses
  2. {
  3. /**
  4.   * @var integer
  5.   *
  6.   * @ORM\Column(name="id_users_email_adresses", type="smallint", nullable=false)
  7.   * @ORM\Id
  8.   * @ORM\GeneratedValue(strategy="IDENTITY")
  9.   */
  10. private $id;
  11.  
  12. /**
  13.   * @var string
  14.   *
  15.   * @ORM\Column(name="email", type="string", length=100, nullable=true)
  16.   */
  17. private $email;
  18.  
  19. /**
  20.   * @var type
  21.   *
  22.   * @ORM\ManyToOne(targetEntity="Users", inversedBy="email")
  23.   * @ORM\JoinColumn(name="users_id", referencedColumnName="id_users")
  24.   */
  25. private $users;


I teraz w deklaracji formularza mam coś takiego
  1. public function buildForm(FormBuilderInterface $builder, array $options)
  2. {
  3. $builder
  4. ->add('name')
  5. ->add('email', 'collection', array(
  6. 'type'=> new UsersEmailAddressesType(),
  7. ))
  8. ;
  9. }
  10.  
  11. public function setDefaultOptions(OptionsResolverInterface $resolver)
  12. {
  13. $resolver->setDefaults(array(
  14. 'data_class' => 'Test\UserBundle\Entity\Users',
  15. 'cascade_validation' => true
  16. ));
  17. }

Jako że przy dodawaniu nowego usera nie ma żadnego adresu email to pole input się nie wyświetla więc na hama dodałem (tak wiem że na stronie jest opcja dodawania pola przez JS)
  1. $email = new \Test\UserBundle\Entity\UsersEmailAddresses;
  2. $formEmail = $this->createForm(new \Test\UserBundle\Form\UsersEmailAddressesType, $email);
  3.  
  4. return array(
  5. 'email_form' => $formEmail->createView(),
  6. 'entity' => $entity,
  7. 'form' => $form->createView(),
  8. );


I wszystko było by ok gdyby nie to że po wysłaniu formularza dostaje komunikat
Cytat
Catchable Fatal Error: Argument 1 passed to Test\UserBundle\Entity\Users::setEmail() must be an instance of Test\UserBundle\Entity\UsersEmailAddresses, array given..

No i teraz pytanie brzmi czy dobrze się do tego zabrałem (IMG:style_emoticons/default/questionmark.gif) Bo jak mam przekazać obiekt UsersEmailAddresses w sytuacji kiedy chce aby najpierw user wpadl do bazy a pozniej adres email z id_users wczesniej dodanego.

Chyba że to działa
insert (email)
id = insert (users)
update (email) set users_id = id
(a tak podejrzewam że to działa)

Ps Sorki za może głupie pytanie ale z ORM do tej pory miałem nie wiele wspólnego.
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
Crozin
post
Post #2





Grupa: Zarejestrowani
Postów: 6 476
Pomógł: 1306
Dołączył: 6.08.2006
Skąd: Kraków

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


1. Po pierwsze nazewnictwo. Dlaczego do reprezentacji pojedynczego użytkownika używasz nazwy Users (zamiast User), zaś do kolekcji maili email zamiast emails? Przecież to nie ma najmniejszego sensu, a kod bardzo źle się czyta.
2. Typ collection domyślnie pozwala jedynie na edycje istniejących elementów kolekcji, nie ich dodawanie czy kasowanie. Musisz ustawić opcje allow_add i allow_delete na TRUE.
3. Podstawowa lektura: dokumentacja typu collection.
4. Możesz też być zainteresowany typem entity oraz How to Embed a Collection of Forms.

Ten post edytował Crozin 28.05.2013, 12:06:51
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: 17.10.2025 - 22:38