Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [Symfony]2 - left join nie redukuje ilości zapytań
Forum PHP.pl > Forum > PHP > Frameworki
duga
Mam problem odnośnie symfony2. Pobrałem dane z użyciem left join (by pobrać jak najmniejszą ilością zapytań). Powinno być u mnie 2 zapytania a są 4. Za każdym razem jak używam getAuthor czy getTags (niezależnie czy jest join, czy nie) to zawsze generuje zapytanie.
Log zapytań:

Cytat
Parameters: { }
Time: 0.68 ms
SELECT a0_.id AS id0, a0_.description AS description1, a0_.content AS content2, a0_.created_at AS created_at3, a0_.updated_at AS updated_at4, a0_.user_id AS user_id5 FROM article a0_ LEFT JOIN user u1_ ON a0_.user_id = u1_.id LEFT JOIN article_tag a3_ ON a0_.id = a3_.article_id LEFT JOIN tag t2_ ON t2_.id = a3_.tag_id WHERE a0_.id = ?
Parameters: ['1']
Time: 0.85 ms
SELECT t0.id AS id1, t0.login AS login2, t0.password AS password3, t0.created_at AS created_at4, t0.updated_at AS updated_at5, t0.name AS name6, t0.lastName AS lastName7 FROM user t0 WHERE t0.id = ?
Parameters: ['1']
Time: 0.82 ms
SELECT t0.id AS id1, t0.name AS name2, t0.created_at AS created_at3, t0.updated_at AS updated_at4 FROM tag t0 INNER JOIN article_tag ON t0.id = article_tag.tag_id WHERE article_tag.article_id = ?
Parameters: [1]
Time: 0.69 ms


Kod z repozytorium article:

  1. <?php
  2.  
  3. namespace tpsa\StoreBundle\Repository;
  4.  
  5. use Doctrine\ORM\EntityRepository;
  6.  
  7. /**
  8.  * articleRepository
  9.  *
  10.  * This class was generated by the Doctrine ORM. Add your own custom
  11.  * repository methods below.
  12.  */
  13. class articleRepository extends EntityRepository
  14. {
  15. function getLimitedArticles ($offset, $count)
  16. {
  17. $qb = $this->createQueryBuilder("a");
  18. $query = $qb->leftJoin("a.author", "u", "ON")->leftJoin("a.tags", "t", "ON")->setFirstResult($offset)->orderBy("a.created_at", "DESC")
  19. ->setMaxResults($count)->getQuery();
  20. $result = $query->getResult();
  21. return $result;
  22. }
  23.  
  24. function getCountArticles ()
  25. {
  26. $query = $this->createQueryBuilder("a")->add("select", "count(a.id)")->getQuery();
  27. $result = $query->getResult();
  28. return $result[0][1];
  29. }
  30.  
  31. function getArticle ($id)
  32. {
  33. $query = $this->createQueryBuilder("a")->select("a")->where ("a.id = :id")->setParameter("id", $id)->leftJoin("a.author", "u", "ON")
  34. ->leftJoin("a.tags", "t", "ON")->getQuery();
  35. $result = $query->getSingleResult();
  36. // print ("<html><body><pre>"); var_dump ($result); print ("</pre></body></html>"); die ();
  37. return $result;
  38. }
  39.  
  40. }


Kod z kontrolera:

  1. <?php
  2.  
  3. namespace tpsa\BlogBundle\Controller;
  4.  
  5. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  6.  
  7.  
  8. class DefaultController extends Controller
  9. {
  10.  
  11. public function indexAction($page)
  12. {
  13.  
  14. // ta będzie podawać listę newsów
  15. // route /{nr strony}
  16. $repo = $this->getDoctrine()->getRepository('tpsaStoreBundle:article');
  17.  
  18. $count = 10;
  19. $offset = ($page - 1) * $count;
  20. $cpages = 1; // TODO: dorób funkcję pobierania liczby artykółów
  21. // wszystkich
  22. // cpages Liczba stron artykułów
  23.  
  24. // TODO: zrobić , aby liczba postów która
  25. // TODO: aktualnie jest przyjęta jako 10 była pobierana z ustawień np. z
  26. // TODO: pliku yaml
  27.  
  28. $countArticles = $repo->getCountArticles ();
  29. $result = $repo->getLimitedArticles ($offset, $count);
  30.  
  31. return $this->render('tpsaBlogBundle:Default:index.html.twig',
  32. array('articles' => $result));
  33. }
  34.  
  35. public function viewAction($id)
  36. {
  37. $repo = $this->getDoctrine()->getRepository('tpsaStoreBundle:article');
  38. $article = $repo->getArticle ($id);
  39. if (!$article) new NotFoundHttpException ('404 Error - Not Found');
  40.  
  41. return $this->render('tpsaBlogBundle:Default:article.html.twig',
  42. array('item' => $article));
  43.  
  44. }
  45. }


Kod z szablonu:

  1. {# src/tpsa/BlogBundle/Resources/views/Default/index.html.twig #}
  2.  
  3. {% extends '::base.html.twig' %}
  4.  
  5. {% block content %}
  6.  
  7. <div class="article">
  8. <div class="header">
  9. {{ item.description }}
  10. </div>
  11. <hr />
  12. <div class="date">
  13. Utworzono: {{ item.createdAt.format('Y-m-d H:i:s') }},
  14. zmodyfikowano: {{ item.updatedAt.format('Y-m-d H:i:s')
  15. }} <br>
  16. autor: {{ item.getAuthor.getLogin }}
  17. </div>
  18. <div class="content">
  19. {{ item.content | truncate(256) }}
  20. <div class="tags">
  21. {% for tagitem in item.tags %}
  22. <a href="">1 - {{ tagitem.name }}</a>
  23. {% endfor %}
  24. </div>
  25. </div>
  26.  
  27. </div>
  28.  
  29.  
  30. {% block articleNavigation %}
  31.  
  32. <select name="operation">
  33. <option value="edit">Eytuj</option>
  34. <option value="delete">Usuń</option>
  35. </select>
  36. <input type="submit" value="wykonaj" /><br>
  37.  
  38. {# Miejsce na kontrolki do nawigowania np. dodaj/edytuj itd. #}
  39.  
  40. {% endblock %}
  41.  
  42. </form>
  43.  
  44.  
  45. {% endblock %}


Co robię, źle? Chciałbym wysyłać jak najmniej zapytań. Na stronie głównej w chwili obecnej (lising newsów) wydawanych jest 6, a listingu artykułu jednego jest 4. (listing zapytań jest dla wyświetlania artykułu)[php][/php]
phpion
Cytat(duga @ 24.09.2011, 10:59:01 ) *
Co robię, źle?

Na pewno dałeś złe bbcode w swoim poście. Proszę użyć odpowiedniego (PHP oraz HTML).
tiraeth
Dodaj pola do wybrania. Narazie tylko joinujesz do zapytania.

Linia 17, przykładowo:
  1. $qb = $this->createQueryBuilder("a")->select(array('a', 'u', 't'));
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.