Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [Cake][PHP] Wyświetla się ID użytkownika zamist nazwy, Wyświetla się ID użytkownika zamist nazwy
Astan
post
Post #1





Grupa: Zarejestrowani
Postów: 28
Pomógł: 0
Dołączył: 16.01.2015

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


Witam,

mam problem przy wyświetlaniu komentarzy, chodzi o to, że nie ukazuje mi się nazwa użytkownika tylko jego id. Wie ktoś jak mogę rozwiązać dany problem ?

Kod z CommentsController

  1. public function add($id = null) {
  2. $userData = $this->Auth->user();
  3.  
  4. if ($this->request->is('post')) {
  5. $this->Comment->create();
  6. if ($this->Comment->save($this->request->data)) {
  7. $this->Session->setFlash(__('Komentarz został dodany.'));
  8. return $this->redirect(array());
  9. } else {
  10. $this->Session->setFlash(__('Komentarz nie został dodany, proszę spróbować ponownie.'));
  11. }
  12. }
  13.  
  14. $users = $this->Comment->User->find('list'));
  15. $posts = $this->Comment->Post->find('list', array('fields' => array('id')));
  16. $postData = $this->Comment->Post->read(null, $id, array('fields' => 'id, name'));
  17.  
  18. $this->set(compact('users', 'posts', 'userData', 'postData'));
  19. }


kawałek kodu z katalogu view/comments/add

  1. <?php foreach ($postData['Comment'] as $comment): ?>
  2. <table id="table_com">
  3. <tr>
  4. <td>
  5. <div class="post">
  6. <p>Użytkownik: <a style="color: #d58512"><?php echo $comment['user_id'] ?></a></p>
  7. <div id="cm_content"><?php echo $comment['content'] ?></div><br /><br />
  8. <?php echo date('Y-m-d H:i', strtotime($comment['date'])) ?>
  9. </div>
  10. </td
  11. </tr>
  12. </table>
  13. <br />
  14. <?php endforeach; ?>



przy użyciu debug

  1. <?php debug($postData); ?>


wyniku otrzymuję:

  1. 'Post' => array(
  2. 'id' => '46',
  3. 'user_id' => '14',
  4. 'title' => 'a',
  5. 'content' => '<p>a</p>
  6. ',
  7. 'date' => '2016-11-14 22:48:00'
  8. ),
  9. 'User' => array(
  10. 'password' => '*****',
  11. 'id' => '14',
  12. 'name' => 'aaa',
  13. 'surname' => 'asd',
  14. 'username' => 'c',
  15. 'email' => 'asdasd@asdasd.pl',
  16. 'information' => '<p>kolorowy świat</p>
  17. ',
  18. 'code' => '552b800c-8fec-4f8e-9637-05a3ce389c84'
  19. ),
  20. 'Comment' => array(
  21. (int) 0 => array(
  22. 'id' => '7',
  23. 'user_id' => '14',
  24. 'post_id' => '46',
  25. 'content' => '<p>Komenatarz 1</p>
  26. ',
  27. 'date' => '2015-04-21 16:02:00'
  28. ),
  29. (int) 1 => array(
  30. 'id' => '8',
  31. 'user_id' => '7',
  32. 'post_id' => '46',
  33. 'content' => '<p>Komenatarz 2</p>
  34. ',
  35. 'date' => '2015-04-21 16:02:00'
  36. ),
  37. (int) 2 => array(
  38. 'id' => '9',
  39. 'user_id' => '14',
  40. 'post_id' => '46',
  41. 'content' => '<p>Komenatarz 3</p>
  42. ',
  43. 'date' => '2015-04-21 16:02:00'
  44. )
  45. )
  46. )


Ten post edytował Astan 21.04.2015, 16:07:38
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 6)
Kshyhoo
post
Post #2





Grupa: Opiekunowie
Postów: 3 855
Pomógł: 317
Dołączył: 4.01.2005
Skąd: że




Zapewne ta linia:
  1. <p>Użytkownik: <a style="color: #d58512"><?php echo $comment['user_id'] ?></a></p>
Go to the top of the page
+Quote Post
Astan
post
Post #3





Grupa: Zarejestrowani
Postów: 28
Pomógł: 0
Dołączył: 16.01.2015

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


Zgadza się, za bardzo nie wiem jak mam poprawić zapytanie kontrolerze aby pobierało nazwę użytkownika.
Model do komentarza

  1. public $belongsTo = array(
  2. 'User' => array(
  3. 'className' => 'User',
  4. 'foreignKey' => 'user_id',
  5. 'conditions' => '',
  6. 'fields' => '',
  7. 'order' => ''
  8. ),
  9. 'Post' => array(
  10. 'className' => 'Post',
  11. 'foreignKey' => 'post_id',
  12. 'conditions' => '',
  13. 'fields' => '',
  14. 'order' => ''
  15. )
  16. );
Go to the top of the page
+Quote Post
Riggs
post
Post #4





Grupa: Zarejestrowani
Postów: 162
Pomógł: 13
Dołączył: 16.06.2007

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


Zamień:
  1. <p>Użytkownik: <a style="color: #d58512"><?php echo $comment['user_id']; ?></a></p>


na

  1. <p>Użytkownik: <a style="color: #d58512"><?php echo $users[$comment['user_id']]; ?></a></p>


Jeśli lista userów pobrała Ci się dobrze to powinno być ok.
Go to the top of the page
+Quote Post
Astan
post
Post #5





Grupa: Zarejestrowani
Postów: 28
Pomógł: 0
Dołączył: 16.01.2015

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


Dzięki za pomoc, działa poprawnie (IMG:style_emoticons/default/smile.gif) na przyszłość będę wiedział jak rozwiązać tego typu problem.
Go to the top of the page
+Quote Post
Pyton_000
post
Post #6





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

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


Pobierając komentarze powinieneś mieć dostęp do relacji. Może masz ustawione brak pobierania zagłębień.
Go to the top of the page
+Quote Post
Riggs
post
Post #7





Grupa: Zarejestrowani
Postów: 162
Pomógł: 13
Dołączył: 16.06.2007

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


Ogólnie to przy pobieraniu danych mógłby ustawić parametr recursive na 2 ale wtedy pobiera się sporo nadmiarowych danych. To co ma teraz powinno wystarczyć.
Go to the top of the page
+Quote Post

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

 



RSS Aktualny czas: 23.08.2025 - 10:18