Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [Symfony]symfony2 - niepotrzebnie dodawany rekord
bartos12
post
Post #1





Grupa: Zarejestrowani
Postów: 9
Pomógł: 0
Dołączył: 21.11.2011

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


Witam,

Ps. Przy edycji usunąłem przez nieuwagę poprzednią cześć ;/

A więc jeszcze raz. Chce aby przy dodawaniu nowego usera pobrało mi tylko id wybranej przeze mnie roli. Jak narazię tworzy role dla każdego nowo utworzonego usera. Jestem niemal pewien że problem tkwi w formularzu.

  1. class UserNewType extends AbstractType
  2. {
  3. public function buildForm(FormBuilder $builder, array $options)
  4. {
  5. $builder->add('userName', 'text');
  6. $builder->add('userPass', 'repeated', array(
  7. 'type' => 'password',
  8. 'first_name' => 'Password',
  9. 'second_name' => 'Re-enter Password',
  10. 'invalid_message' => "The passwords don't match!"
  11. ));
  12. $builder->add('userFirstname', 'text');
  13. $builder->add('userLastname', 'text');
  14. $builder->add('role', new RoleType());
  15. }
  16.  
  17. public function getName()
  18. {
  19. return 'user';
  20. }
  21.  
  22. public function getDefaultOptions(array $options)
  23. {
  24. return array(
  25. 'data_class' => 'Acme\FootballBundle\Entity\User',
  26. );
  27. }
  28. }


  1. class RoleType extends AbstractType
  2. {
  3. public function buildForm(FormBuilder $builder, array $options)
  4. {
  5. $builder->add('role', 'entity', array(
  6. 'class' => 'AcmeFootballBundle:Role',
  7. 'query_builder' => function($er){
  8. return $er->createQueryBuilder('u')->orderBy('u.roleId', 'ASC');
  9. }
  10. ));
  11. }
  12.  
  13. public function getDefaultOptions(array $options)
  14. {
  15. return array(
  16. 'data_class' => 'Acme\FootballBundle\Entity\Role',
  17. );
  18. }
  19.  
  20. public function getName()
  21. {
  22. return 'role';
  23. }
  24. }


i jeszcze controller

  1. $user = new User();
  2. $form = $this->createForm(new UserNewType(), $user);
  3.  
  4. if($request->getMethod() == 'POST')
  5. {
  6. $form->bindRequest($request);
  7. if($form->isValid())
  8. {
  9. $em = $this->getDoctrine()->getEntityManager();
  10. $em->persist($user);
  11. $em->flush();
  12.  
  13. return $this->redirect($this->generateUrl('_userIndex'));
  14. }
  15. }
  16.  
  17. return $this->render('AcmeFootballBundle:User:add.html.twig', array('form' => $form->createView()));


Ten post edytował bartos12 30.12.2011, 18:07:15
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: 19.09.2025 - 03:33