Reputation: 2442
After fill a form with two adresses: Billing address and personal address(using FormHelper):
...
<div><?php echo $this->Form->input('Contact.1.name'); ?></div></td>
<td> <div>name1</div>
<div><?php echo $this->Form->input('Contact.2.name'); ?></div></td>
...
, i get this array:
'Contact' => array(
(int) 1 => array(
'name' => 'asdf',
'nif' => '123123123',
'address' => 'pcsa',
'zipcode' => '1234',
'street' => 'das'
),
(int) 2 => array(
'name' => 'fsad',
'nif' => '321321321',
'address' => 'asdp',
'zipcode' => '1234',
'street' => 'fas'
)
)
But before i save this array to send data for DB i need to set manually user_id=32 (for example).
Can you give me any suggestion how to do that? so i can get this result:
'Contact' => array(
(int) 1 => array(
'name' => 'asdf',
'nif' => '123123123',
'address' => 'pcsa',
'zipcode' => '1234',
'street' => 'das',
'user_id' => 32 <----------------
),
(int) 2 => array(
'name' => 'fsad',
'nif' => '321321321',
'address' => 'asdp',
'zipcode' => '1234',
'street' => 'fas',
'user_id' => 39 <---------------
)
)
My purpose here is to save data from a form to a table "contacts" for the same user_id. for that reason i can't use user_id as a input form, i need to do that after form and before inserte on my DB. For that reason, how can i set this array with user_id?
Upvotes: 0
Views: 1091
Reputation: 2442
thanks a lot for your suggestions, but this is the final solution for me.
$this->request->data['Contact'][1]['user_id'] = 32;
$this->request->data['Contact'][2]['user_id'] = 39;
Simply as that, i didn't see it because i was debugging wrong.
Upvotes: 0
Reputation: 7592
Checkout the CakePHP Hash library you can do stuff like
$this->request->data = Hash::insert($this->request->data, 'Contact.{n}.user_id', 123);
Example
$contacts = array(
'Contact' => array(
array(
'name' => 'asdf',
'nif' => '123123123',
'address' => 'pcsa',
'zipcode' => '1234',
'street' => 'das'
),
array(
'name' => 'fsad',
'nif' => '321321321',
'address' => 'asdp',
'zipcode' => '1234',
'street' => 'fas'
)
)
);
$contacts = HASH::insert($contacts, 'Contact.{n}.user_id', 123);
pr($contacts);
Output
Array
(
[Contact] => Array
(
[0] => Array
(
[name] => asdf
[nif] => 123123123
[address] => pcsa
[zipcode] => 1234
[street] => das
[user_id] => 123
)
[1] => Array
(
[name] => fsad
[nif] => 321321321
[address] => asdp
[zipcode] => 1234
[street] => fas
[user_id] => 123
)
)
)
Upvotes: 2
Reputation: 6066
Try this
foreach ($this->request->data['Contact'] as &$contact) {
$contact['user_id'] = 32;
}
Upvotes: 1