Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP+Symfony] Zapis danych z pola typu "select"
Forum PHP.pl > Forum > PHP
dopelganger
Witam
mam problem z zapisaniem wartości wybranej z listy wyboru <select></select> - parentId. Pojawia się komunikat:

[2/2] DBALException: An exception occurred while executing 'INSERT INTO menu (sort, parentId, enabled, label, link) VALUES (?, ?, ?, ?, ?)' with params [6, {}, 0, "fhgfgffh", "fghfghfhfh"]:
Catchable Fatal Error: Object of class BackendBundle\Entity\Menu could not be converted to string

Czepia się o parentId :/
Prosze o pomoc.

w Entity: Menu.php mam:
  1.  
  2. /**
  3.   * @ORM\Column(type="integer")
  4.   */
  5. private $parentId;
  6.  
  7. public function getParentId()
  8. {
  9. return $this->parentId;
  10. }
  11.  
  12. public function setParentId($parentId)
  13. {
  14. $this->parentId = $parentId;
  15. }


MenuType.php:
(Lista select pobierana jest z bazy)
  1. public function buildForm(FormBuilderInterface $builder, array $options)
  2. {
  3. $builder
  4. // ...
  5. ->add('parentId','entity', array(
  6. 'class' => 'BackendBundle\Entity\Menu',
  7. 'query_builder' => function(EntityRepository $er) {
  8. return $er->createQueryBuilder('q')->orderBy('q.sort', 'ASC');
  9. },
  10. 'property' => 'label',
  11. 'required' => false
  12. ))
  13. // ...
  14. ;
  15. }


MenuController:
  1.  
  2. /**
  3.   * @Route("/", name="admin_menu_create")
  4.   * @Method("POST")
  5.   * @Template("BackendBundle:Menu:new.html.twig")
  6.   */
  7. public function createAction(Request $request)
  8. {
  9. $entity = new Menu();
  10. $form = $this->createCreateForm($entity);
  11. $form->handleRequest($request);
  12.  
  13. if ($form->isValid()) {
  14. $em = $this->getDoctrine()->getManager();
  15. $em->persist($entity);
  16. $em->flush();
  17.  
  18. return $this->redirect($this->generateUrl('admin_menu_show', array('id' => $entity->getId())));
  19. }
  20.  
  21. return array(
  22. 'entity' => $entity,
  23. 'form' => $form->createView(),
  24. );
  25. }
rad11
A dlaczego kolumne masz typu Integer a próbujesz dodać obiekty {} ?
dopelganger
Cytat(rad11 @ 21.04.2016, 13:35:50 ) *
A dlaczego kolumne masz typu Integer a próbujesz dodać obiekty {} ?


Jak dodam __toString to nie wywala tego błędu ale nie zapisuje wartości do tabeli - jest NULL.

object(Symfony\Component\HttpFoundation\ParameterBag)#8 (1) {
["parameters":protected]=>
array(1) {
["backendbundle_menu"]=>
array(6) {
["submit"]=>
string(0) ""
["_token"]=>
string(43) "NRolS8JFgx-kmp9J3ek8O-rf4i4MLtUlZUUtJyxnrFs"
["label"]=>
string(17) "test"
["parentId"]=>
string(1) "1"
["link"]=>
string(11) "test"
["sort"]=>
string(2) "2"
}
}
}

wartość parentId jest (jak widać), ale nie zapisuje jej wartości do tabeli :/

przykladowo dam tak:

  1. $entity->setParentId(2);
i zapisze , więc jak pobrać tego POSTA :/ ?????????????????????????

Ten zapis również daje NULL:

  1. $pid = $this->get('request')->request->get('parentId');
  2. var_dump($pid);
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.