2012-02-02 40 views
0

我有一个模型Tasklist,并且每个Tasklist项目都属于一个Service项目。CakePHP - 使用group by来打印单个表格标题

在我的任务列表控制器:

function index() { 
    $this->Tasklist->recursive = 0; 
    $this->set('tasklists', $this->Tasklist->find('all', array(
    'order' => array(
     'Service.name' => 'ASC', 
     'Tasklist.name' => 'ASC' 
    ) 
))); 
} 
我简单的索引视图

相关部分:

<?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; ?> 

而不是每个表格行的打印服务名称,我想打印出来作为与任务列表分组在每个下面:

<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> 

我已经尝试过使用组p参数在我的控制器和嵌套的foreach在我看来,但不能得到它的工作。

回答

1

我会做这样的

$current_service = ''; 
    <?php foreach ($tasklists as $tasklist): ?> 
    <?php if($current_service != $tasklist['Service']['name']): ?> 
    <tr> 
      <th colspan="5"><?php echo $this->Html->link($tasklist['Service']['name'], array('controller' => 'services', 'action' => 'view', $tasklist['Service']['id'])); ?></th> 
    </tr> 
    <?php $current_service = $tasklist['Service']['name']; ?> 
    <?php endif; ?> 
    <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> 

    <?php endforeach; ?> 
+0

啊,完美。谢谢。 – randomswathe 2012-02-02 21:09:16