I have the output from my controller as shown below:
D:\web\www2\application\src\CommonBundle\Controller\ClassesController.php:22:
array (size=2)
'Grade 8' =>
array (size=1)
0 =>
array (size=2)
0 =>
object(CommonBundle\Entity\GradeLevel)[363]
...
1 =>
object(CommonBundle\Entity\GradeLevel)[367]
...
'Grade 9' =>
array (size=1)
0 =>
array (size=1)
0 =>
object(CommonBundle\Entity\GradeLevel)[372]
...
This data is coming from a One-T-_Many relation and what I want to do is have a grouping on my html table. Below is how I create my array of object:
$grades = $this->getDoctrine()->getRepository('CommonBundle:Grade')->findAll();
$classes = array();
foreach($grades as $grade){
$classes[$grade->getName()][] = $this->getDoctrine()->getRepository('CommonBundle:GradeLevel')->findBy(array(
'grade' => $grade->getId()
));
}
return $this->render('CommonBundle:Classes:index.html.twig', array(
'classes' => $classes
));
If this was a plain PHP code I would simple do something like this:
<?php
foreach($classes as $key => $values){
...
<td colspan="4"><?php echo $key //Being the category name as indexed from the controller ?></td>
//And continue with the contents of the classes
foreach($values as $class){
<tr>
<td><?php echo $class->getName(); ?>
....
}
}
I am finding it hard getting any solutiuon on the web to suite my requirement. Can anybody point me to a right direction as the TWIG documentation seems to be abstract on this as well.
EDIT:
Below is the current table structure that I have to make it easy for any helper to have an idea of what I am trying to achieve.
<table class="table">
<thead>
<tr>
<th>#</th>
<th>Class</th>
<th>Created on</th>
<th>Modified at</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{% for values in classes %}
<tr>
<!--- Category name should go here e.g "Grade 8" then next loop should list all Grades from that category -->
<td colspan="5" align="right">{{ values }}</td>
</tr>
{% for item in values %}
<tr>
<td> </td>
<td>{{ item.name }}</td>
<td>{{ item.created|date('Y-m-d H:i:s') }}</td>
<td>{{ item.modified|date('Y-m-d H:i:s') }}</td>
<td><a class="btn btn-sm btn-primary" href="#" role="button">Summary</a></td>
</tr>
{% endfor %}
{% endfor %}
</tbody>
EDIT 2
