2012-09-22 40 views
0

所以,我所擁有的是顯示帖子的頁面。帖子被收集到項目中。而且,在這個頁面上,除了文章外,我還要顯示同一個項目的上一篇和下一篇文章。所以,我有這個在我的控制器:獲取相同類別的前一個/下一個帖子

$this->set("post", $this->Post->find("first", array("conditions" => array("Post.id" => $id), "contain" => array("User", "Project", "Project.Post" => array("id", "image", "title"), "Comment", "Comment.User" => array("id", "username", "mail"), "Comment.User.Post" => array("id", "image", "title"))))); 
$this->set("neighbors", $this->Post->find("neighbors", array("field" => "id", "value" => $id))); 

的問題是,它從所有的崗位,不僅同一項目的那些拿到一個和下一個職位。

所以,如果你能幫助我一點:)

回答

2

我假設郵政屬於關聯項目和項目的hasMany帖子。

您將只需要添加一個額外的條件:

$post = $this->Post->find("first", ...); // this is your first find call, assign it to a var instead 

$neighbors = $this->Post->find('neighbors', array(
    'field' => 'id', 
    'value' => $id, 
    'conditions' => array(
     'Post.project_id' => $post['Post']['project_id'], 
    ), 
)); 
$this->set(compact('post', 'neighbors')); 

我還沒有嘗試過,但看着源,findNeighbors不兌現conditions關鍵。

+0

是的,我正在嘗試這樣的事情,問題是我不知道如何從控制器內的原始帖子的$ projectId。 – Axiol

+0

查看我編輯的答案 – tigrang

+0

看起來不錯。謝謝 :) – Axiol

相關問題