Parcourir tableau d'objets
Bonjour,
j'ai quelque soucis a parcourir dans mon template twig un tableau retourné par ma requête :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <?php
public function myFindAll()
{
$genres = $this->createQueryBuilder('g')
// leftJoin because I need all the genre
->leftJoin('g.films', 'f')
->addSelect('COUNT(f)')
->groupBy('g.id')
->getQuery()
->getResult();
// $genres contains all the genres and the associated movies
return ($genres);
} |
Je l'appelle dans mon controller et je fais un :
Qui me génère ceci :
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
| <?php
array (size=5)
0 =>
array (size=2)
0 =>
object(gstyle39\VideothequeBundle\Entity\Genre)[224]
private 'id' => int 1
protected 'films' =>
object(Doctrine\ORM\PersistentCollection)[219]
...
protected 'label' => string 'Action' (length=6)
1 => string '2' (length=1)
1 =>
array (size=2)
0 =>
object(gstyle39\VideothequeBundle\Entity\Genre)[221]
private 'id' => int 2
protected 'films' =>
object(Doctrine\ORM\PersistentCollection)[230]
...
protected 'label' => string 'Aventure' (length=8)
1 => string '1' (length=1)
2 =>
array (size=2)
0 =>
object(gstyle39\VideothequeBundle\Entity\Genre)[228]
private 'id' => int 3
protected 'films' =>
object(Doctrine\ORM\PersistentCollection)[233]
...
protected 'label' => string 'Combat' (length=6)
1 => string '1' (length=1) |
D'habitude je procède avec un for :
Code:
1 2 3 4 5 6 7 8
| <ul>
{% for genre in genres %}
<li>
<a href="{{ path('VideothequeBundle_genre', {'id': genre.id}) }}">{{ genre.label }}</a>
Nombre de films associés : (ici je veux aussi afficher la valeur du 2ème index de chaque array (exemple : string '2' pour le premier array))
</li>
{% endfor %}
</ul> |
Ce qui me renvoie :
Citation:
Item "id" for "Array" does not exist in gstyle39VideothequeBundle:Videotheque:s_genres.html.twig at line 7
Merci d'avance pour vos explications.