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:
<?php
namespace tpsa\StoreBundle\Repository;
use Doctrine\ORM\EntityRepository;
/**
* articleRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class articleRepository extends EntityRepository
{
function getLimitedArticles ($offset, $count)
{
$qb = $this->createQueryBuilder("a");
$query = $qb->leftJoin("a.author", "u", "ON")->leftJoin("a.tags", "t", "ON")->setFirstResult($offset)->orderBy("a.created_at", "DESC")
->setMaxResults($count)->getQuery();
$result = $query->getResult();
return $result;
}
function getCountArticles ()
{
$query = $this->createQueryBuilder("a")->add("select", "count(a.id)")->getQuery();
$result = $query->getResult();
return $result[0][1];
}
function getArticle ($id)
{
$query = $this->createQueryBuilder("a")->select("a")->where ("a.id = :id")->setParameter("id", $id)->leftJoin("a.author", "u", "ON")
->leftJoin("a.tags", "t", "ON")->getQuery();
$result = $query->getSingleResult();
// print ("<html><body><pre>"); var_dump ($result); print ("</pre></body></html>"); die ();
return $result;
}
}
Kod z kontrolera:
<?php
namespace tpsa\BlogBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class DefaultController extends Controller
{
public function indexAction($page)
{
// ta będzie podawać listę newsów
// route /{nr strony}
$repo = $this->getDoctrine()->getRepository('tpsaStoreBundle:article');
$count = 10;
$offset = ($page - 1) * $count;
$cpages = 1; // TODO: dorób funkcję pobierania liczby artykółów
// wszystkich
// cpages Liczba stron artykułów
// TODO: zrobić , aby liczba postów która
// TODO: aktualnie jest przyjęta jako 10 była pobierana z ustawień np. z
// TODO: pliku yaml
$countArticles = $repo->getCountArticles ();
$result = $repo->getLimitedArticles ($offset, $count);
return $this->render('tpsaBlogBundle:Default:index.html.twig',
array('articles' => $result)); }
public function viewAction($id)
{
$repo = $this->getDoctrine()->getRepository('tpsaStoreBundle:article');
$article = $repo->getArticle ($id);
if (!$article) new NotFoundHttpException ('404 Error - Not Found');
return $this->render('tpsaBlogBundle:Default:article.html.twig',
array('item' => $article));
}
}
Kod z szablonu:
{# src/tpsa/BlogBundle/Resources/views/Default/index.html.twig #}
{% extends '::base.html.twig' %}
{% block content %}
{{ item.description }}
Utworzono: {{ item.createdAt.format('Y-m-d H:i:s') }},
zmodyfikowano: {{ item.updatedAt.format('Y-m-d H:i:s')
autor: {{ item.getAuthor.getLogin }}
{{ item.content | truncate(256) }}
{% for tagitem in item.tags %}
<a href="">1 - {{ tagitem.name }}
</a> {% endfor %}
{% block articleNavigation %}
<input type="submit" value="wykonaj" /><br>
{# Miejsce na kontrolki do nawigowania np. dodaj/edytuj itd. #}
{% endblock %}
{% 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]
Ten post edytował duga 24.09.2011, 10:37:44