Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP+Symfony] dodanie rekordu
dopelganger
post
Post #1





Grupa: Zarejestrowani
Postów: 236
Pomógł: 0
Dołączył: 27.10.2012

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


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. }


Ten post edytował dopelganger 4.02.2016, 15:28:43
Go to the top of the page
+Quote Post
aras785
post
Post #2





Grupa: Zarejestrowani
Postów: 859
Pomógł: 177
Dołączył: 29.10.2009

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


Cześć.

Może to pomoże: http://stackoverflow.com/a/20911267
Go to the top of the page
+Quote Post
dopelganger
post
Post #3





Grupa: Zarejestrowani
Postów: 236
Pomógł: 0
Dołączył: 27.10.2012

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


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.
Go to the top of the page
+Quote Post
maly_swd
post
Post #4





Grupa: Zarejestrowani
Postów: 744
Pomógł: 118
Dołączył: 14.02.2009
Skąd: poziome

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


Do testu zamiaast $entity->setUserId($this->getUser()->getId()); podstaw konkretną wartość np.1 i zobacz czy się utrwala w bazie.


--------------------
śmieszne obrazki
Kryzys: Ser jem spleśniały, wino piję stare i samochód mam bez dachu..
Go to the top of the page
+Quote Post
ohm
post
Post #5





Grupa: Zarejestrowani
Postów: 623
Pomógł: 144
Dołączył: 22.12.2010

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


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
Go to the top of the page
+Quote Post
dopelganger
post
Post #6





Grupa: Zarejestrowani
Postów: 236
Pomógł: 0
Dołączył: 27.10.2012

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


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. }


Ten post edytował dopelganger 5.02.2016, 12:09:10
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 22.08.2025 - 08:20