Question

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?

Was it helpful?

Solution

TLDR

There are any number of ways to accomplish what you want. The most ideal solution would be to add the id into the view, similar to how you mentioned. But - there are as many options as you can think of.


In View

The easiest (and I would argue the ideal) solution would just be to add the id in your view. You should be able to just add it like this:

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

It makes it hidden automatically since it's "id" and it should populate for you as well if you're passing the data correctly to the view.


In Controller

BUT - since your question was how to do it NOT in the view, another option is to do it in the Controller. I don't know your exact project, so change it as you see fit, but before your save, you can always just append the Block's id to the data.

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

In Model

This seems like a strange place, but - you if you're storing the ID in session or something, you COULD theoretically add the id here before saving as well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top