Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [SF][Symfony2][SF2] Relacje ManyToOne
Forum PHP.pl > Forum > PHP > Frameworki
gentleman
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.
pedro84
A pokaż encje i te relacje. Składowa getUsers() zapewne zwraca kolekcję.
gentleman
  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.
pedro84
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.
gentleman
Poprawiłem, nie sądzisz że powinienem zmienić metodę, ponieważ findOneBy() wyszukuje pojedynczy wynik. Co mógłbym wstawić za to?
pedro84
Ale Ty chcesz pobrać jeden komentarz czy wszystkie?
gentleman
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.
pedro84
  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ś.
gentleman
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
pedro84
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.
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.