CakePHP问题:我怎么能通过一个帖子/视图的id评论/视图


CakePHP question: How can i pass the id of a post/view to comment/view?

我现在在posts/view/id上。

可以编辑、删除posts/view/id中的评论。

如果我点击任何评论的编辑链接,那么将执行comments/view/id。

编辑完注释后我需要做的=>

$this->redirect(array('controller'=>'posts','action' => 'view',$id));

这里$id将是帖子的id。

如何在评论/查看操作中获得帖子的id。

这是comments/edit/id action =>

function edit($id) {
    $this->Comment->id = $id;
    if (empty($this->data)) {
        $this->data = $this->Comment->read();
    } else {
        if ($this->Comment->save($this->data)) {
            $this->Session->setFlash('Your comment has been updated.');
            $this->redirect(array('controller'=>'posts','action' => 'view',$id));
        }
    }
}

这是评论的编辑链接=>

echo $this->Html->link('Edit', array('controller'=>'comments','action'=>'edit',$comment['Comment']['id']));

在评论/视图中不应该有$this->data['Comment']['post_id']字段吗?除非你在发帖和评论之间有一些非正统的关系。所以$this->redirect(array('controller'=>'posts','action' => 'view',$this->data['Comment']['post_id']));

我认为你的评论数据库表定义了一个外键字段,导致评论所属的帖子?我想,至少这是这种情况下的最佳实践。如果你有这样的实现,你可以很容易地获得post_id通过使用这个外键字段你当前查看的评论。

然而,您可能已经选择了一种不同的方法来连接posts和模型。我认为使用该连接将是获取post_id的最简单方法。但是我们需要更多关于你的实现的信息。