2010-12-14 95 views
0

如果我從路由URL訪問它們,我無法爲分頁結果分頁。這些是我使用的路線:CakePHP 1.3.6中的分頁+路徑問題

// NEWS 
Router::connect('/news.rss', array('controller' => 'posts', 'action' => 'index', 'ext' => 'rss')); 
Router::connect('/news/*', array('controller' => 'posts', 'action' => 'index')); 
Router::connect('/:lang/posts/*', array('controller' => 'posts', 'action' => 'index')); 

我知道,在過去的路線,我沒有通過:郎參數,但如果我將它傳遞:

Router::connect('/:lang/news/*', array('controller' => 'posts', 'action' => 'index'), array('lang' => $regex['lang'], 'pass' => array('lang'))); 

它不工作。

如果我嘗試訪問url/news/page:2,它會顯示第一頁的結果。我打印出來,這 - $> PARAMS,看它是否正確需要的頁碼,並在一審中,它的作用:

Array 
(
[lang] => ca 
[named] => Array 
    (
     [page] => 2 
    ) 

[pass] => Array 
    (
    ) 

[controller] => posts 
[action] => index 
[plugin] => 
[url] => Array 
    (
     [ext] => html 
     [url] => ca/posts/page:2 
    ) 

[form] => Array 
    (
    ) 
[...] 
) 

陣列的這部分(我中省略了一些零件,我會以後告訴你)是相同的,如果我訪問/新聞/頁:2和/職位/索引/頁:2,但如果你看看這部分:

Array 
(
[...] 
[paging] => Array 
    (
     [Post] => Array 
      (
       [page] => 1 
       [current] => 3 
       [count] => 3 
       [prevPage] => 
       [nextPage] => 
       [pageCount] => 1 
       [defaults] => Array 
        (
         [limit] => 3 
         [step] => 1 
         [order] => Post.created DESC 
         [conditions] => Array 
          (
           [Post.active] => 1 
           [Post.page] => 
           [0] => Post.public_date <= NOW() 
          ) 

        ) 

       [options] => Array 
        (
         [page] => 1 
         [limit] => 3 
         [order] => Post.created DESC 
         [conditions] => Array 
          (
           [Post.active] => 1 
           [Post.page] => 
           [0] => Post.public_date <= NOW() 
          ) 

        ) 

      ) 

    ) 

你可以看到,它不」請正確輸入頁碼。但是,如果我從/ posts/index/page訪問,它需要編號和分頁工作。

如果只是很漂亮的網址不打擾我,但考慮到該網站是多語言的,我至少需要那個作品,如果我訪問/ en/posts/index/page:2(或/ en/news/page 2)...

這裏是我的全部routes.php文件文件:

http://pastebin.com/th4hLZNz

任何人有什麼正在發生的想法?

在此先感謝

回答

0

最後我找到了解決方案。其實這很容易,最大的問題是我沒有正確地集中它。我專注于思考我做錯了什麼的路線,但問題是控制器。

這裏是問題:

$this->paginate['conditions'] = array(
    'Post.active' => true, 
    'Post.page' => $page, 
    'Post.public_date <= NOW()'); 

$this->paginate['group'] = 'Post.id'; 

的組(在convination的條件)不允許paginateCount函數計算的結果良好。所以我創建缺少組條件對郵政的型號我自己paginateCount功能:

/** 
* This method fixes the count query 
* when there's a GROUP BY on it 
* 
* @return integer 
*/ 
public function paginateCount($conditions = null, $recursive = 0, $extra = array()) 
{ 
    $parameters = compact('conditions', 'recursive'); 
    $params = array(); 
    if (isset($extra['group'])) 
    { 
     $params = array_merge(array(
      'fields' => array(
       'COUNT(DISTINCT(' . $this->alias . '.' . $this->primaryKey . ')) AS `count`' 
      ) 
     ), $parameters); 
    } 
    else 
    { 
     $params = array_merge($parameters, $extra); 
    } 
    return $this->find('count', $params); 
} 

而現在看來效果不錯