2014-10-02 87 views
0

我有我的控制器PAGINATE結果沒有給予正確的結果

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')功能的基礎上一場的結果。該查詢返回正確的數據,並且如果我在渲染視圖之前對其進行了處理,則信息是正確的。但是,在使用$ 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> 

回答

0

您沒有使用分頁正確,

分頁比正常查找查詢不同,他們兩個服務於不同的目的。檢查url 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')); 

這將是正確的做事方式。

雖然find('all')是正常查找查詢並且與paginator無關