所以,我所拥有的是一个显示帖子的页面。帖子被收集到项目中。而且,在这个页面上,除了帖子之外,我还要显示同一个项目的上一篇和下一篇文章。所以,我在我的控制器中有这个:
$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)));
问题在于它从所有帖子中获取上一个和下一个帖子,而不仅仅是同一个项目的帖子。
所以,如果你能帮助我一点:)
答案 0 :(得分: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
密钥。