2011-01-29 83 views
1

爲什麼沒有工作按「作者」排序?不會引發任何錯誤,只是未排序使用加入cakephp排序問題

function index() 
     { 
      $this->paginate = array(
        'limit' => 5, 
        'order' => array(
         'Post.id' => 'asc', 
        ), 
        'fields' => array('Post.id', 'Post.title', 'User.name AS aut_name'), 
        'joins' => array(
         array(
          'table' =>'users', 
          'alias' =>'User', 
          'type' =>'LEFT', 
          'conditions' => array(
           'Post.author = User.id' 
          ) 
         ) 
        ) 
      ); 
      $posts = $this->paginate(); 
      $this->set('posts', $posts); 
     } 

<tr> 
     <th><?php echo $paginator->sort('ID', 'id'); ?></th> 
     <th><?php echo $paginator->sort('Author', 'User.name'); ?></th> 
     <th><?php echo $paginator->sort('Tilte', 'title'); ?></th> 
     <th>Actions</th> 
    </tr> 
+3

您的代碼表示,它的排序由Post.id,不是作者 – chustar 2011-01-29 21:08:40

+0

但當我點擊「<?php echo $ paginator-> sort('Tilte','title');?> 」按標題排序,那​​爲什麼不按排序方式排序當我點擊<?php echo $ paginator-> sort('Author','User.name'); ?>? – lolalola 2011-01-30 14:50:08

回答

1

的代碼表明User.name領域正在被返回爲「aut_name」,也許設置排序參數使用該字段的名字呢?

<th><?php echo $paginator->sort('Author', 'aut_name'); ?></th> 
1

也許你應該做這樣 在控制器:

'fields' => array('Post.id', 'Post.title', 'User.name') 

鑑於:

<th><?php echo $paginator->sort('Author', 'User.name'); ?></th>