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)));

问题是它从所有帖子中获取上一个和下一个帖子,而不仅仅是同一个项目的帖子。

所以,如果你能帮助我一点:)

4

1 回答 1

2

我假设 Post belongsTo Project 和 Project hasMany Post。

您只需要添加一个额外的条件:

$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密钥。

于 2012-09-22T17:46:14.717 回答