Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [SF][Symfony2][SF2] Relacje ManyToOne
gentleman
post
Post #1





Grupa: Zarejestrowani
Postów: 41
Pomógł: 1
Dołączył: 17.05.2013

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


Witam, chciałbym Was prosić o pomoc. Jestem mało doświadczony w sf2, ostatnio natrafiłem na pewien problem.Pobierając komentarze nie jestem w stanie wyświetlić ich autora. Wiem że korzystając z findOneBy() pobieram tylko 1 rekord, który wyświetla się w pozostałych.
  1.  
  2. $entity = $this->getDoctrine()
  3. ->getRepository('AcmeMainBundle:Comment')
  4. ->findOneBy(array( 'post_id' => $postId) );
  5.  
  6. $a_comment = $entity->getUsers()->getName();


Jeśli wiecie jaki jest problem prosiłbym o wskazówkę dla korekty lub o nowe rozwiązanie.

Ten post edytował gentleman 29.11.2013, 20:57:58
Go to the top of the page
+Quote Post
pedro84
post
Post #2





Grupa: Nieautoryzowani
Postów: 2 249
Pomógł: 305
Dołączył: 2.10.2006

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


A pokaż encje i te relacje. Składowa getUsers() zapewne zwraca kolekcję.

Ten post edytował pedro84 29.11.2013, 21:53:54


--------------------
Google knows the answer...
Go to the top of the page
+Quote Post
gentleman
post
Post #3





Grupa: Zarejestrowani
Postów: 41
Pomógł: 1
Dołączył: 17.05.2013

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


  1. class Comment {
  2. /**
  3.   * @ORM\Id
  4.   * @ORM\Column(type="integer")
  5.   * @ORM\GeneratedValue(strategy="AUTO")
  6.   */
  7. protected $id;
  8.  
  9.  
  10. /**
  11.   * @ORM\Column(type="string", length=255)
  12.   */
  13.  
  14. protected $user_id;
  15.  
  16.  
  17.  
  18. /**
  19.   * @ORM\Column(type="string")
  20.   */
  21.  
  22.  
  23. protected $content;
  24.  
  25.  
  26. /**
  27.   * @ORM\Column(type="string", length=255)
  28.   */
  29.  
  30. protected $date;
  31.  
  32.  
  33.  
  34. /**
  35.   * @ORM\Column(type="string", length=255)
  36.   *
  37.   */
  38.  
  39. protected $post_id;
  40.  
  41. /**
  42.   * @ORM\ManyToOne(targetEntity="Users", inversedBy="comment")
  43.   * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
  44.   */
  45.  
  46. protected $users;
  47.  
  48. //....
  49.  
  50. /**
  51.   * Set users
  52.   *
  53.   * @param \Acme\MainBundle\Entity\Users $users
  54.   * @return Comment
  55.   */
  56. public function setUsers(\Acme\MainBundle\Entity\Users $users = null)
  57. {
  58. $this->users = $users;
  59.  
  60. return $this;
  61. }
  62.  
  63. /**
  64.   * Get users
  65.   *
  66.   * @return \Acme\MainBundle\Entity\Users
  67.   */
  68. public function getUsers()
  69. {
  70. return $this->users;
  71. }
  72. }



  1. class Users
  2. {
  3. /**
  4.   * @ORM\Id
  5.   * @ORM\Column(type="integer")
  6.   * @ORM\GeneratedValue(strategy="AUTO")
  7.   */
  8. protected $id;
  9.  
  10. /**
  11.   * @ORM\Column(type="string", length=255)
  12.   */
  13. protected $email;
  14.  
  15. /**
  16.   * @ORM\Column(type="string", length=255)
  17.   */
  18. protected $username;
  19.  
  20. /**
  21.   * @ORM\Column(type="string", length=255)
  22.   */
  23. protected $location;
  24.  
  25. /**
  26.   * @ORM\Column(type="string", length=255)
  27.   */
  28. protected $name;
  29.  
  30. /**
  31.   * @ORM\Column(type="string", length=255)
  32.   */
  33. protected $password;
  34.  
  35. /**
  36.   * @ORM\Column(type="integer")
  37.   */
  38. protected $role = 1;
  39.  
  40. /**
  41.   * @ORM\OneToMany(targetEntity="Comment", mappedBy="users")
  42.   */
  43. protected $comment;
  44.  
  45. // ....
  46.  
  47. /**
  48.   * Constructor
  49.   */
  50. public function __construct()
  51. {
  52. $this->comment = new ArrayCollection();
  53. }
  54.  
  55. /**
  56.   * Add comment
  57.   *
  58.   * @param \Acme\MainBundle\Entity\Comment $comment
  59.   * @return Users
  60.   */
  61. public function addComment(\Acme\MainBundle\Entity\Comment $comment)
  62. {
  63. $this->comment[] = $comment;
  64.  
  65. return $this;
  66. }
  67.  
  68. /**
  69.   * Remove comment
  70.   *
  71.   * @param \Acme\MainBundle\Entity\Comment $comment
  72.   */
  73. public function removeComment(\Acme\MainBundle\Entity\Comment $comment)
  74. {
  75. $this->comment->removeElement($comment);
  76. }
  77.  
  78. /**
  79.   * Get comment
  80.   *
  81.   * @return \Doctrine\Common\Collections\Collection
  82.   */
  83. public function getComment()
  84. {
  85. return $this->comment;
  86. }
  87. }


czy jest możliwość obejścia relacji 1:n i zrobić coś typu:
Kod
$repository = $this->getDoctrine()
                ->getRepository('AcmeMainBundle:Comment');
        $query = $repository->createQueryBuilder('c')
                ->where('c.post_id ='. $topicId)
                ->getQuery();

        $comment_user = $query->getSingleResult()->getUserId();


Kod
$repository = $this->getDoctrine()
                ->getRepository('AcmeMainBundle:Users');
        $query = $repository->createQueryBuilder('u')
                ->where('u.id ='. $comment_user)
                ->getQuery();

        $name = $query->getSingleResult()->getUsername();


W obu przypadkach wszystko jest ok gdy komentarz jest tylko 1, jeśli dochodzą kolejne jest coś nie tak.

Ten post edytował gentleman 29.11.2013, 22:10:09
Go to the top of the page
+Quote Post
pedro84
post
Post #4





Grupa: Nieautoryzowani
Postów: 2 249
Pomógł: 305
Dołączył: 2.10.2006

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


W kwestii nazewnictwa, to masz na odwrót - w encji User komentarze powinny mieć nazwę w liczbie mnogiej ($comments, nie $comment - sam ją nawet deklarujesz jako kolekcję), zaś w encji Comment $user, a nie $users, bo to pojedynczy obiekt.

Sam zapis masz ok, tylko po tych poprawkach powinien wyglądać:
  1. // $entity to zwrócony z bazy obiekt Comment
  2. $author = $entity->getUser()->getName();
  3. echo $author;


Działać musi.


--------------------
Google knows the answer...
Go to the top of the page
+Quote Post
gentleman
post
Post #5





Grupa: Zarejestrowani
Postów: 41
Pomógł: 1
Dołączył: 17.05.2013

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


Poprawiłem, nie sądzisz że powinienem zmienić metodę, ponieważ findOneBy() wyszukuje pojedynczy wynik. Co mógłbym wstawić za to?
Go to the top of the page
+Quote Post
pedro84
post
Post #6





Grupa: Nieautoryzowani
Postów: 2 249
Pomógł: 305
Dołączył: 2.10.2006

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


Ale Ty chcesz pobrać jeden komentarz czy wszystkie?


--------------------
Google knows the answer...
Go to the top of the page
+Quote Post
gentleman
post
Post #7





Grupa: Zarejestrowani
Postów: 41
Pomógł: 1
Dołączył: 17.05.2013

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


Chciałbym wszystkie. Mam pobrane wszystkie komentarze, chciałbym zamienić id_usera na nazwę użytkownika. Przepraszam jeżeli się źle w 1 poście wyraziłem.

Ten post edytował gentleman 29.11.2013, 22:29:12
Go to the top of the page
+Quote Post
pedro84
post
Post #8





Grupa: Nieautoryzowani
Postów: 2 249
Pomógł: 305
Dołączył: 2.10.2006

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


  1. // to jest post
  2. $entity = $this->getDoctrine()->getRepository('AcmeMainBundle:Post)->find($postId);
  3.  
  4. $comments = $post->getComments();
  5.  
  6. // kolekcja komentarzy
  7. foreach ($comments as $comment) {
  8. echo $comment->getUser()->getUsername() . PHP_EOL;
  9. }

Uwaga: musisz tylko zdefiniować relację pomiędzy obiektami Post <=> Comment, bo tego nie masz, a winieneś.

Ten post edytował pedro84 29.11.2013, 22:37:26


--------------------
Google knows the answer...
Go to the top of the page
+Quote Post
gentleman
post
Post #9





Grupa: Zarejestrowani
Postów: 41
Pomógł: 1
Dołączył: 17.05.2013

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


Dziękuję za pomoc.

jednak coś nie działa. Zwraca mi taki błąd:
Notice: Undefined index: posts in /workspace/praca/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 1575


ale czy to nie jest to samo co tamto??
  1. $comments = $this->getDoctrine()
  2. ->getRepository('AcmeMainBundle:Comment')
  3. ->findOneBy(array('post_id' => $topicId));
  4.  
  5. $ta = $comments->getUser();
  6.  
  7. foreach($ta as $to){
  8.  
  9. $tar = $to->getName();
  10.  
  11. }


zwraca że $tar jest niezdefiniowany

Ten post edytował gentleman 30.11.2013, 19:44:18
Go to the top of the page
+Quote Post
pedro84
post
Post #10





Grupa: Nieautoryzowani
Postów: 2 249
Pomógł: 305
Dołączył: 2.10.2006

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


Chłopie, zobacz co Ty robisz. Zobacz co zawiera ta zmienna:
Kod
$ta = $comments->getUser();
. findOneBy() zwraca jeden określony obiekt, w tym przypadku komentarz. Teraz, za pomocą metody getUser() uzyskujesz dostęp do obiektu User zdefinowanego w relacji. To zawiera zmienna $ta. Teraz chcesz po niej przejechać pętlą. Widzisz już błąd?

Pomijam już to, że stosujesz całkowicie mylącą konwencję nazewnictwa zmiennych, relacji.


--------------------
Google knows the answer...
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: 20.08.2025 - 07:57