Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [Symfony][SF2][Symfony2]Associations
gentleman
post
Post #1





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

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


Cześć.

Od niedawna bawię się z Symfony2 i mam problem z pobraniem nazwy autora postu. Jakos do tej pory nie mialem problemów smile.gif

Kawałek entity Users:
  1. /**
  2.  * @ORM\Entity
  3.  * @ORM\Table(name="users")
  4.  */
  5. class Users
  6. {
  7. /**
  8.   * @ORM\OneToMany(targetEntity="Comments", mappedBy="users")
  9.   */
  10. protected $comments;
  11.  
  12. public function __construct()
  13. {
  14. $this->comments = new ArrayCollection();
  15. }
  16.  
  17. // ....
Oraz Comments:
  1. /**
  2.  * @ORM\Entity
  3.  * @ORM\Table(name="comments")
  4.  */
  5. class Comments {
  6.  
  7. /**
  8.   * @ORM\ManyToOne(targetEntity="Users", inversedBy="users")
  9.   * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
  10.   */
  11.  
  12. protected $users;
  13.  
Wiem że to prymitywny kod ale czytając dokumentacje dowiedziałem się że tak moge pobrać moja nazwe usera. Więc w kontrolerze zrobilem taka funkcje:
  1. public function Username($id) {
  2. $name = $this->getDoctrine()
  3. ->getRepository('AcmeForumBundle:Comments')
  4. ->find($id);
  5.  
  6. $username = $name->getUsers()->getName();
  7.  
  8. return $username;
  9. }

ale nic sie nie dzieje.W czym polega błąd?

Ten post edytował gentleman 6.07.2013, 11:56:17
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 5)
404
post
Post #2





Grupa: Zarejestrowani
Postów: 226
Pomógł: 25
Dołączył: 22.05.2011

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


Hmmm, a czy to nie powinno być tak:
  1. //...
  2. $username = $name->getUserId()->getName();
  3. //...

?
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%)
-----


za każdym razem wyrzuca błąd; brak argumentu.Linia: public function Username($id) {
a jest jakiś inny sposób ?

@robie tak samo jak w dokumentacji tylko wlasny przyklad

Ten post edytował gentleman 6.07.2013, 13:37:51
Go to the top of the page
+Quote Post
destroyerr
post
Post #4





Grupa: Zarejestrowani
Postów: 879
Pomógł: 189
Dołączył: 14.06.2006
Skąd: Bytom

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


Cytat
za każdym razem wyrzuca błąd; brak argumentu.Linia: public function Username($id) {

Czyli problem dotyczy wywołania funkcji Username. Nie przesyłasz do niej argumentu.
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%)
-----


czyli entity dobrze zrobiłem tylko jest problem z argumentem??
Go to the top of the page
+Quote Post
poh
post
Post #6





Grupa: Zarejestrowani
Postów: 7
Pomógł: 1
Dołączył: 12.12.2008

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


a getUsers() / setUsers() jak wygladaja?
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: 19.08.2025 - 20:19