Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [SF2][Symfony] Wyświetlenie danych z bazy na każdej stronie
Lirdoner
post
Post #1





Grupa: Zarejestrowani
Postów: 500
Pomógł: 1
Dołączył: 29.09.2009

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


Witam, chcę w headerze wyświetlić ilość nieprzeczytanych przez użytkownika wiadomości. Muszę to zrobić w dwóch miejscach.
Mam taki element w widoku
  1. <li class="nav-item dropdown">
  2. <a class="nav-link dropdown-toggle" href="#" id="profile" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
  3. <span class="badge badge-success">LICZBA WIADOMOŚCI</span>
  4. {{ app.user.username }}
  5. </a>
  6. <div class="dropdown-menu" aria-labelledby="profile">
  7. <a class="dropdown-item" href="#">
  8. Wiadomości
  9. <span class="badge badge-success">LICZBA WIADOMOŚCI</span>
  10. </a>
  11. <a class="dropdown-item" href="{{ path('logout') }}">Wyloguj</a>
  12. {% if is_granted('ROLE_ADMIN') %}
  13. <a class="dropdown-item" href="{{ path('admin_homepage') }}">Panel administracyjny</a>
  14. {% endif %}
  15. </div>
  16. </li>

robiąc {{ render(controller(...)) }} niepotrzebnie dwa razy wykonuje to samo zapytanie do bazy. Jaki inny sposób polecacie?
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
Lirdoner
post
Post #2





Grupa: Zarejestrowani
Postów: 500
Pomógł: 1
Dołączył: 29.09.2009

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


Dzięki za pomoc, stworzyłem już serwis z takim kodem
  1. <?php
  2.  
  3. namespace AppBundle\Model;
  4.  
  5. use Doctrine\ORM\EntityManager;
  6. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
  7.  
  8. class User
  9. {
  10. private $em;
  11. private $user = null;
  12.  
  13. private static $userdata = array();
  14.  
  15. public function __construct(EntityManager $em, TokenStorage $token)
  16. {
  17. if($token = $token->getToken())
  18. $this->user = $token->getUser();
  19.  
  20. $this->em = $em;
  21. }
  22.  
  23. public function getUnreadMessagesCount()
  24. {
  25. if(isset( self::$userdata['unreadMessagesCount'] ))
  26. return self::$userdata['unreadMessagesCount'];
  27.  
  28. self::$userdata['unreadMessagesCount'] = (...)
  29.  
  30. return self::$userdata['unreadMessagesCount'];
  31. }
  32. }

Problem w tym, że od czasu do czasu dostaję wyjątek "Error: Call to a member function getEmail() on null". getEmail() to funkcja z $this->user->getEmail().
Jestem poprawnie zalogowany na stronie.
services.yml:
Kod
app.user:
    class: AppBundle\Model\User
    arguments:    ['@doctrine.orm.entity_manager', '@security.token_storage']


Ten post edytował Lirdoner 8.02.2017, 16:20:20
Go to the top of the page
+Quote Post

Posty w temacie


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: 16.10.2025 - 16:59