2012-11-15 36 views
3

首先讓我告訴我,我是cakePHP的完整初學者。 我已經在cakePHP中創建了一個名爲students的表,我只想擁有可排序的標題,所以我使用了paginator幫助類。不幸的是,我收到以下警告,我只是不明白爲什麼,加上排序不起作用。無法對cakePHP中的數據進行排序和分頁

Warning (2) 
: array_filter() expects parameter 1 to be array, null given  
[CORE\Cake\View\Helper\PaginatorHelper.php, line 395] 

Warning (2) 
: array_merge() [ 
function.array-merge 
]: Argument #1 is not an array [CORE\Cake\View\Helper\PaginatorHelper.php, line 395] 

這裏是我的控制器

enter code here 
<?php 
class StudentsController extends AppController{ 
    public $helpers = array('Html', 'Form', 'Session', 'Paginator'); 
    public $components = array('Session', 'Paginator'); 



    public function index(){ 
     $this->set('students', $this->Student->find('all')); 
    } 

    public function view($id = NULL){ 
     $this->Student->id = $id; 
     $this->set('student', $this->Student->read()); 
    } 

    public function add(){ 
     if($this->request->is('post')){ 
      $this->Student->create(); 
      if($this->Student->save($this->request->data)){ 
       $this->Session->setFlash('The student has been added.'); 
       $this->redirect(array('action' => 'index')); 
      }else{ 
       $this->Session->setFlash('Unable to add the student'); 
      } 
     } 
    } 

    public function edit($id = NULL){ 
     $this->Student->id = $id; 
     if($this->request->is('get')){ 
      $this->request->data = $this->Student->read(); 
     }else{ 
      if($this->Student->save($this->request->data)){ 
       $this->Session->setFlash('The student has been edited'); 
       $this->redirect(array('action' => 'index')); 
      }else{ 
       $this->Session->setFlash('Unable to edit the student'); 
      } 
     } 
    } 

    public function delete($id = NULL){ 
     if($this->request->is('get')){ 
      throw new MethodNotAllowedException; 
     }else{ 
      if($this->Student->delete($id)){ 
       $this->Session->setFlash('The Student has been succesfully deleted'); 
       $this->redirect(array('action' => 'index')); 
      } 
     } 
    } 
} 

?>

這是我的觀點實際上我的索引

<h1>Welcome to the Students Page</h1> 

<?php echo $this->Html->link('Add Student', array('controller' => 'students', 'action' => 'add')); ?> 

    <table> 
     <tr> 
      <th><?php echo $this->Paginator->sort('id', 'Matriculation Number'); ?></th> 
      <th>Last Name</th> 
      <th>First Name</th> 
      <th>Course Of Studies</th> 
      <th>Email</th> 
      <th>Remark</th> 
     </tr> 
     <?php 
      foreach($students as $student): 
     ?> 
     <tr> 
      <td><?php echo $student['Student']['id']?></td> 
      <td><?php echo $student['Student']['last_name']?></td> 
      <td><?php echo $student['Student']['first_name']?></td> 
      <td><?php echo $student['Student']['course_of_studies']?></td> 
      <td><?php echo $student['Student']['email']?></td> 
      <td><?php echo $student['Student']['remark']?></td> 
      <td><?php echo $this->Html->link($student['Student']['id'], array('controller' => 'students', 'action' => 'view', $student['Student']['id'])); ?> 
      <td><?php echo $this->Html->link('Edit', array('controller' => 'students', 'action' => 'edit', $student['Student']['id']));?></td> 
      <td><?php echo $this->Form->postLink('Delete', array('controller' => 'students', 'action' => 'delete', $student['Student']['id']), array('confirm' => 'Are you sure ?')); ?></td> 
     </tr> 
     <?php endforeach; ?> 
      <?php unset($student); ?> 
    </table> 

我也用以下

<?php echo $this->Paginator->sort('id', 'Matriculation Number', array('model' => 'Student')); ?> 
<?php echo $this->Paginator->sort('Student.id', 'Matriculation Number'); ?> 

我仍然得到相同的錯誤。我在這裏錯過了很簡單的事情嗎?因爲我一直在谷歌搜索幾個小時沒有運氣....

回答

3

你實際上並沒有使用Paginator組件,因此它沒有設置助手使用的需要的請求數據。更改索引功能看起來像

public function index(){ 
    $this->set('students', $this->paginate()); 
    // or $this->set('students', $this->Paginator->paginate()); 
} 
0

有2種方法可以做到這一點。

方法1:在不使用Paginatior

class StudentsController extends AppController { 

    public $paginate = array(); 

    public function index() {  
     ... 
     $this->paginate['order'] = array('Student.created' => 'desc'); 
     $this->paginate['conditions'] = array('Student.active' => 1); 
     $this->paginate['limit'] = 10; 
     $this->set('students', $this->paginate()); 

     ... 
    } 
} 

方法2:使用分頁程序

class StudentsController extends AppController { 

    public $helpers = array('Paginator'); 
    public $components = array('Paginator'); 
    public $paginate = array(
     'limit' => 25, 
     'order' => array(
      'Student.created' => 'desc' 
     ) 
    ); 

    public function index() {  
     ... 
     $this->Paginator->settings = $this->paginate; 
     $this->set('students', $this->Paginator->paginate());  
     ... 
    } 
} 
相關問題