我在数据库中有两个表,cards
和comments
。我已经启动并运行了我的CakePHP应用程序。当在卡片上查看.ctp .. 相关注释显示在页面底部。
我想单击添加新评论,但专门为当前卡添加它,例如预填充卡字段以显示当前类别。
这是我当前查看的新评论链接:
<?php
echo $this->Html->link(__('New comment'), array('controller' => 'comments','action' => 'add'));
?>
这是我的添加注释控制器:
public function add() {
if ($this->request->is('post')) {
$this->Comment->create();
if ($this->Comment->save($this->request->data)) {
$this->Session->setFlash(__('The comment has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The comment could not be saved. Please, try again.'));
}
}
$cards = $this->Comment->Card->find('list');
$this->set(compact('cards'));
}
如何让新添加的评论卡设置为当前卡?
我还希望能够添加新评论,但卡片空白..准备好供用户从列表中选择。
您可以将 cartId 附加到视图页面上的链接:
array(..., 'action'=>'add', $cartId)
然后你可以按如下方式检索它:
public function add($cartId = null) {
//...
if (isPost) {
//
} else {
$this->request->data['Comment']['cart_id'] = $cartId;
}
//...
}
这样,cartId 将填充 GET 上的选择框(在 POST 上,您希望使用已发布的参数来确保表单在验证错误的情况下记住其发布的值(