I have a model Tasklist, and each Tasklist item belongs to a Service item.
In my Tasklist controller:
function index() {
$this->Tasklist->recursive = 0;
$this->set('tasklists', $this->Tasklist->find('all', array(
'order' => array(
'Service.name' => 'ASC',
'Tasklist.name' => 'ASC'
)
)));
}
Relevant part of my simple index View:
<?php foreach ($tasklists as $tasklist): ?>
<tr>
<td><?php echo $tasklist['Tasklist']['id']; ?></td>
<td><?php echo $tasklist['Tasklist']['name']; ?></td>
<td>
<?php echo $this->Html->link($tasklist['Service']['name'], array('controller' => 'services', 'action' => 'view', $tasklist['Service']['id'])); ?>
</td>
<td><?php echo $tasklist['Tasklist']['created']; ?></td>
<td><?php echo $tasklist['Tasklist']['modified']; ?></td>
<td>
<?php echo $this->Html->link(__('Edit', true), array('action' => 'edit', $tasklist['Tasklist']['id']), array('class' => 'button edit')); ?>
</td>
</tr>
<?php endforeach; ?>
Instead of printing the Service name in every table row I would like to print it as a with the Tasklists grouped beneath each one:
<tr>
<th colspan="5"><?php echo $this->Html->link($tasklist['Service']['name'], array('controller' => 'services', 'action' => 'view', $tasklist['Service']['id'])); ?></th>
</tr>
<tr>
<td><?php echo $tasklist['Tasklist']['id']; ?></td>
<td><?php echo $tasklist['Tasklist']['name']; ?></td>
<td><?php echo $tasklist['Tasklist']['created']; ?></td>
<td><?php echo $tasklist['Tasklist']['modified']; ?></td>
<td>
<?php echo $this->Html->link(__('Edit', true), array('action' => 'edit', $tasklist['Tasklist']['id']), array('class' => 'button edit')); ?>
</td>
</tr>
I have experimented with using the group parameter in my controller and a nested foreach in my view, but cannot get it to work.
I’d do it like this