QueryBuilder avec count()
Salut à tous !
Je suis entrain de développer un forum, je suis à l'étape ou je souhaite récupérer le nombre de topic par forum et de l'afficher.
Je procède comme ceci :
ForumController.php :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| <?php
namespace Sds\ForumBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class ForumController extends Controller
{
public function indexAction()
{
$em = $this->getDoctrine()->getEntityManager();
$repository = $em->getRepository('SdsForumBundle:Categorie');
$listeCategories = $repository->getCategorieAvecForums();
$em = $this->getDoctrine()->getEntityManager();
$repository = $em->getRepository('SdsForumBundle:Forum');
$listeNbTopics = $repository->getNbTopicParForums();
return $this->render('SdsForumBundle:Forum:index.html.twig', array('listeCategories' => $listeCategories,
'listeNbTopics' => $listeNbTopics));
}
} |
getCategorieAvecForums() :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| <?php
namespace Sds\ForumBundle\Entity;
use Doctrine\ORM\EntityRepository;
/**
* CategorieRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class CategorieRepository extends EntityRepository
{
public function getCategorieAvecForums()
{
$qb = $this->createQueryBuilder('c')
->join('c.forums', 'f')
->addSelect('f')
->orderBy('c.ordre', 'ASC');
return $qb->getQuery()
->getArrayResult();
}
} |
getNbTopicParForums :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| <?php
namespace Sds\ForumBundle\Entity;
use Doctrine\ORM\EntityRepository;
/**
* ForumRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class ForumRepository extends EntityRepository
{
public function getNbTopicParForums()
{
$qb = $this->createQueryBuilder('f')
->join('f.topics', 't')
->addSelect('COUNT(t)')
->groupBy('f.id');
return $qb->getQuery()
->getScalarResult();
}
} |
Ma vue :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| {# src/Sds/ForumBundle/Resources/views/Forum/index.html.twig #}
{% extends "SdsSiteBundle::layout.html.twig" %}
{% block title %}Forum - {{ parent() }}{% endblock %}
{% block sdssite_body %}
<div id="forum">
{% for categories in listeCategories %}
<div id="globalforum">
<div id="catforums">
<table class="categories">
<thead>
<th style="text-align: left; width: 340px;"><a href="">{{ categories.nom }}</a></th>
<th style="width: 80px;">SUJETS</th>
<th style="width: 80px;">MESSAGES</th>
<th style="text-align: center; width: 240px;">DERNIER MESSAGE</th>
</thead>
{% for forums in categories.forums %}
<tbody class="forums">
<tr>
<td>
<a href="">{{ forums.name }}</a><br />
{{ forums.description }}
</td>
<td style="text-align: center;">
{% for nbtopic in listeNbTopics %}
{{ nbtopic }}
{% endfor %}
</td>
<td style="text-align: center;">test</td>
<td style="text-align: center;">test</td>
</tr>
</tbody>
{% endfor %}
</table>
</div>
</div>
{% endfor %}
</div>
{% endblock %} |
Voici l'erreur qu'il en découle :
Citation:
An exception has been thrown during the rendering of a template ("Notice: Array to string conversion in /homepages/44/d319387792/htdocs/skydreamsoft/app/cache/dev/twig/56/72/ccd192858f95c3814baf804398e6.php line 88") in ::layout.html.twig at line 128.
Si quelqu'un aurait une idée ?
Je vous remercie d'avance !