paginate导致cakephp没有给出正确的结果

时间:2014-10-02 15:07:39

标签: cakephp pagination

我的控制器中有以下功能

public function getDeleted(){

    $options = array(
            'conditions' => array('deleteFlag' => 1
                ));
    $assets = $this->Asset->find('all', $options);
    $this->set('assets', $this->paginate());
    $this->render('index');
}

我使用它来根据一个字段过滤find('all')函数的结果。查询返回正确的数据,如果在呈现视图之前我debug($assets),则信息是正确的。但是,在我使用$ this-> paginate()之后,它返回表中的所有行,而不是过滤后的结果。谁能告诉我为什么会这样?

如果我将paginate函数保留在set方法之外,则页面会抛出错误。我的观点如下:

<div class="assets index">
<h2>
    <!-- <?php echo __('Assets'); ?> -->
</h2>
<table cellpadding="0" cellspacing="0">
    <thead>
        <tr>
            <th><?php echo $this->Paginator->sort('id'); ?></th>
            <th><?php echo $this->Paginator->sort('fleet_id'); ?></th>
            <th><?php echo $this->Paginator->sort('type_id'); ?></th>
            <th><?php echo $this->Paginator->sort('make_id'); ?></th>
            <th><?php echo $this->Paginator->sort('model'); ?></th>
            <th><?php echo $this->Paginator->sort('fleetNumber', 'Fleet No'); ?>
            </th>
            <th><?php echo $this->Paginator->sort('registrationNumber', 'Reg No'); ?>
            </th>
            <th><?php echo $this->Paginator->sort('chassisNumber', 'Chassis No'); ?>
            </th>
            <th><?php echo $this->Paginator->sort('status_id'); ?></th>
            <th><?php echo $this->Paginator->sort('operator'); ?></th>
            <th><?php echo $this->Paginator->sort('created', 'Created date'); ?>
            </th>
            <th><?php echo $this->Paginator->sort('createdBy', 'Created by'); ?>
            </th>
            <th><?php echo $this->Paginator->sort('modified', 'Modified date'); ?>
            </th>
            <th><?php echo $this->Paginator->sort('modifiedBy', 'Modified by'); ?>
            </th>
            <th><?php echo $this->Paginator->sort('deleteFlag', 'Deleted?'); ?>
            </th>
            <th class="actions"><?php echo __('Actions'); ?></th>
        </tr>
    </thead>
    <tbody>
    <?php foreach ($assets as $asset): ?>
        <tr>
            <td><?php echo $this->Html->link(__($asset['Asset']['id']), array('action' => 'view', $asset['Asset']['id'])); ?>&nbsp;</td>
            <td><?php echo h($asset['Fleet']['name']); ?>&nbsp;</td>
            <td><?php echo h($asset['Type']['name']); ?>&nbsp;</td>
            <td><?php echo h($asset['Make']['name']); ?>&nbsp;</td>
            <td><?php echo h($asset['Asset']['model']); ?>&nbsp;</td>
            <td><?php echo h($asset['Asset']['fleetNumber']); ?>&nbsp;</td>
            <td><?php echo h($asset['Asset']['registrationNumber']); ?>&nbsp;</td>
            <td><?php echo h($asset['Asset']['chassisNumber']); ?>&nbsp;</td>
            <td><?php echo h($asset['Status']['name']); ?>&nbsp;</td>
            <td><?php echo h($asset['Asset']['operator']); ?>&nbsp;</td>
            <td><?php echo h($this->Time->format(($asset['Asset']['created']),'%d/%m/%Y %H:%M')); ?>&nbsp;</td>
            <td><?php echo h($asset['Asset']['createdBy']); ?>&nbsp;</td>
            <td><?php echo h($this->Time->format(($asset['Asset']['modified']),'%d/%m/%Y %H:%M')); ?>&nbsp;</td>
            <td><?php echo h($asset['Asset']['modifiedBy']); ?>&nbsp;</td>
            <td><?php if($asset['Asset']['deleteFlag'] == 0){
                echo h('Active');
            }
            elseif($asset['Asset']['deleteFlag'] == 1){
                echo h('Deleted');  
            }
            ?>&nbsp;</td>

            <td class="actions">
                <?php echo $this->element('buttons',array('values' => $asset)); ?>
            </td>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>

<p>
<?php
echo $this->Paginator->counter(array(
'format' => __('Page {:page} of {:pages}, showing {:current} records out of {:count} total, starting on record {:start}, ending on {:end}')
));
?>
</p>
<div class="paging">
<?php
echo $this->Paginator->prev('< ' . __('previous'), array(), null, array('class' => 'prev disabled'));
echo $this->Paginator->numbers(array('separator' => ''));
echo $this->Paginator->next(__('next') . ' >', array(), null, array('class' => 'next disabled'));
?>
</div>

1 个答案:

答案 0 :(得分:0)

您没有正确使用分页,<​​/ p>

Paginate与普通查找查询不同,其中两个用于不同目的。检查网址http://book.cakephp.org/2.0/en/core-libraries/components/pagination.html#query-setup以了解应如何使用分页组件。

 $this->Paginator->settings = array(
        'conditions' => array('Recipe.deleteFlag' => 1),
        'limit' => 10
    );
    $assets = $this->Paginator->paginate('Recipe');
    $this->set(compact('assets'));

这是做事的正确方法。

虽然查找(&#39;所有&#39;)是正常查找查询并且与分页器无关