In a beforeMarshall()
, I create an additionnal field in my entity.
In my controller, I debug($myEntity)
just before setting the data to the view, and I see my field containing the expected value.
I even created a getter like follow in myEntity.php
:
protected function _getTmpImage() {
if (isset($this->_properties['tmp_image'])) {
debug($this->_properties['tmp_image']);
return $this->_properties['tmp_image'];
} else {
return null;
}
}
And In my view, I see the corresponding getter’s debug()
display.
But the following line:
<?= $this->Form->hidden('agpoi_images.'.$key.'.tmp_image'); ?>
desesperately creates the following html code:
<input name="agpoi_images[0][tmp_image]" class="form-control" value="" type="hidden">
Can you tell me why value is empty?
PS: I also tried to add the following statement in my entity class, even if I think I don’t have to do that according to the doc
class MyEntity extends Entity
{
protected $_virtual = ['tmp_image'];
}
post your debug(), and Form->create()
Thanks jarekgol,
Form is created like that:
<?= $this->Form->create($agPoi, ['type' => 'file'])?>
Debug() displays:
object(ArrayObject) {
. . .
agpoi_images => [
(int) 0 => [
'id' => '1045',
'agpoi_id' => '736',
'image' => '59215ec4e5f7e.jpg',
'tmp_image' => '59215ec4e5f7e.jpg',
'sequence' => '0',
'copyright' => '',
'select_audio' => '0',
'select_time' => '01/06/2017 00:00'
]
],
. . .
}
In fact if I specify the value as below, that works. So I wonder why I have to specify its proper value and why CakePHP doesn’t do it itself as for other fields in this object.
<?php foreach ($agPoi->agpoi_images as $key => $agpoi_image):?>
<?= $this->Form->hidden('agpoi_images.'.$key.'.image'); ?> // correctly displays `image` value by itself.
<?= $this->Form->hidden('agpoi_images.'.$key.'.tmp_image', ['value' => $agpoi_image->tmp_image]); ?>
. . .
<?php endif;?>
I search some of my code and found same solution
foreach($orders as $k=>$o) ... echo $this->Form->hidden($k.'.id',['value' => $o->id ]);
except that I wasn’t using virtual fields. So I can’t help you much more.
ps. in other try to build a form with many inputs, I rewrite result set from sql, so every key is an ID. It simplifies batch saving then and you don’t need to hidden(id).