0

I have BlocksController and Block model. In I used edit action to edit my application's blocks and it worked fine.

After migration to 1.3 and 2.x I noticed that the edit action leads to saving an edited block as a new record.

In the cakePHP docs I read that in 1.3 the form's helper does not supply the id any more and so the model regarded the process as add.

To solve this issue, I tried to add a hidden field named id with a value of the id of the block is being edited as follows:

<?php echo $this->Form->create('Block', array('class' => 'nice custom'));?>
//The following line is required in cakephp 1.3+

<?php echo $this->Form->hidden('id', array('value' => $block['Block']['id']));?>

The described solution is working fine. However I need to know another way to do that without changing in the view. Does it possible?

4

1 回答 1

2

TLDR

有多种方法可以实现您想要的。最理想的解决方案是将 id 添加到视图中,类似于您提到的方式。但是 - 有尽可能多的选择。


在视图中

最简单的(我认为是理想的)解决方案就是在您的视图中添加 id 。您应该可以像这样添加它:

echo $this->Form->input('id');

它会自动隐藏,因为它是“id”,如果您将数据正确传递给视图,它也应该为您填充。


在控制器中

但是-由于您的问题是如何不在视图中执行此操作,因此另一种选择是在控制器中执行此操作。我不知道您的确切项目,因此请根据需要进行更改,但在保存之前,您始终可以将块附加id到数据中。

$this->request->data['Block']['id'] = $YOUR_ID;
$this->Block->save($this->request->data);

在模型中

这似乎是一个奇怪的地方,但是 - 如果您将 ID 存储在会话或其他内容中,理论上您也可以在保存之前在此处添加 id。

于 2013-10-04T14:51:46.490 回答