0

我有帖子控制器,我可以通过他们的 ID 路由到每个帖子。

有没有办法从 URL 中获取 id 以将该 id 存储在Commentspost_id外键字段中?

如果是的话,我可以给点提示或一点帮助吗?

4

1 回答 1

0

假设 CakePHP 2.0 和 URL: APP/posts/view/1, 在你的view.ctp

您可以使用 URL 访问所有信息debug($this->request->url);

根据您想要提供的信息pass,因此:

$post_id = $this->request->url['pass'][0];

将此值传递给表单中的隐藏字段,其中的操作addCommentsController

echo $this->Form->input('post_id', array('type'=>'hidden', 'value'=>$post_id));

当你提交表单时,会有一个数组键data[Comment][post_id] // 1

然后它将comments使用正确的post_id外键填充表。

于 2012-10-16T20:57:54.403 回答