Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP+Symfony] dodanie rekordu
Forum PHP.pl > Forum > PHP
dopelganger
Witam
nie wiem o co chodzi,
dodawanie rekordu działa ok, ALE nie zapisuje ID usera z sesji (FOSUserBundle).
Prosze o pomoc.

Kontroler:
  1. /**
  2.   * Creates a new Post entity.
  3.   *
  4.   * @Route("/", name="admin_posts_create")
  5.   * @Method("POST")
  6.   * @Template("BackendBundle:Post:new.html.twig")
  7.   */
  8. public function createAction(Request $request)
  9. {
  10. $entity = new Post();
  11. $form = $this->createCreateForm($entity);
  12. $form->handleRequest($request);
  13.  
  14. if ($form->isValid()) {
  15. $entity->setUserId($this->getUser()->getId());
  16. $em = $this->getDoctrine()->getManager();
  17. $em->persist($entity);
  18. $em->flush();
  19.  
  20. return $this->redirect($this->generateUrl('admin_posts_show', array('id' => $entity->getId())));
  21. }
  22.  
  23. return array(
  24. 'entity' => $entity,
  25. 'form' => $form->createView(),
  26. );
  27. }


Entity Post.php:

  1.  
  2. /**
  3.   * @ORM\Column(name="user_id", type="integer")
  4.   */
  5. private $user_id;
  6.  
  7. public function getUserId()
  8. {
  9. return $this->user_id;
  10. }
  11.  
  12. public function setUserId($user_id)
  13. {
  14. $this->user_id = $user_id;
  15. }
aras785
Cześć.

Może to pomoże: http://stackoverflow.com/a/20911267
dopelganger
Cytat(aras785 @ 4.02.2016, 15:41:00 ) *
Cześć.

Może to pomoże: http://stackoverflow.com/a/20911267


jak printuje $this->getUser()->getId() to wyświetla id więc to nie o to chodzi,,, to jest git.
maly_swd
Do testu zamiaast $entity->setUserId($this->getUser()->getId()); podstaw konkretną wartość np.1 i zobacz czy się utrwala w bazie.
ohm
Cytat(maly_swd @ 5.02.2016, 10:02:12 ) *
Do testu zamiaast $entity->setUserId($this->getUser()->getId()); podstaw konkretną wartość np.1 i zobacz czy się utrwala w bazie.


Albo zrób zwyczajnie relacje ManyToOne i "zapisuj" użytkownika, bez kombinacji z userId
dopelganger
Cytat(maly_swd @ 5.02.2016, 10:02:12 ) *
Do testu zamiaast $entity->setUserId($this->getUser()->getId()); podstaw konkretną wartość np.1 i zobacz czy się utrwala w bazie.


poddaje sie,
zapisałem to tak (może i po lamersku), ale działa:)

  1. if ($form->isValid()) {
  2. $em = $this->getDoctrine()->getManager();
  3. $em->persist($entity);
  4. $em->flush();
  5.  
  6. // Update user_id
  7. $entity = $em->getRepository('BackendBundle:Post')->find($entity->getId());
  8. $entity->setUserId($this->getUser()->getId());
  9. $em->persist($entity);
  10. $em->flush();
  11.  
  12. ...
  13. }
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.