find()cakephp中的Order by
表单位(id、单位编号、汇总) id|单元号find()cakephp中的Order by,cakephp,Cakephp,表单位(id、单位编号、汇总) id|单元号 1 | 23 2 | 25 3 | 22 在UnitsController中: public function index() { $this->set('units', $this->Unit->find('all'), array( 'order' => array('Unit.unitNo ASC'), 'fields' => array('Unit.id', 'Unit.u
1 | 23
2 | 25
3 | 22
在UnitsController中:
public function index() {
$this->set('units', $this->Unit->find('all'), array(
'order' => array('Unit.unitNo ASC'),
'fields' => array('Unit.id', 'Unit.unitNo'))
);
}
index.ctp
<table>
<tr>
<th>Unit</th>
<th>Summary</th>
</tr>
<!-- Here is where we loop through our $posts array, printing out post info -->
<?php foreach ($units as $unit): ?>
<tr>
<td>
<?php echo $this->Html->link($unit['Unit']['unitNo'], array('controller' => 'units', 'action' => 'view', $unit['Unit']['id']));
?>
</td>
<td><?php echo $unit['Unit']['summary']; ?></td>
</tr>
<?php endforeach; ?>
<?php unset($unit); ?>
</table>
单位
总结
我想使用order unitNo,但结果仍然是按id排序。
有人能帮我解决这个问题吗?请尝试一下:
public function index() {
$this->set('units', $this->Unit->find('all',
array(
'order' => array('Unit.unitNo ASC'),
'fields' => array('Unit.id', 'Unit.unitNo')
)
));
}
在我看来,您已经将订单设置作为第三个参数指定给了$this->set()
,但您可能要做的是将其作为第二个参数指定给$this->Unit->find()