Saving data in multiple tables

Hello,

I know my problem is a question which has been asked already quite often, but the answers given didnt help me, unfortunately.
Currently I just set up an example database for understanding. I have 2 tables users and phone_Numbers.
The UsersModel has a hasMany association with PhoneNumbers and PhoneNumbers a belongsTo association with the UsersModel.
In my Users\add.ctp I created a form for saving data in both tables and simply added

<?= __('Add Phone Number ') ?> <?php echo $this->Form->control('phone_numbers.number'); echo $this->Form->control('phone_numbers.type'); ?>

In the UsersController I added the association

$user = $this->Users->patchEntity($user, $this->request->getData(), [

            'associated' => ['PhoneNumbers']
        ]);

When typing the data into my form, only the datas for the users table are saved but not the one for phone_numbers.
Making use of the debug function I noticed an error in the field user_id, which is the foreignKey in my phone_numbers table:

‘phone_numbers’ => [
(int) 0 => object(App\Model\Entity\PhoneNumber) {
‘number’ => ‘0123456789’,
‘type’ => ‘m’,
‘[new]’ => true,
‘[accessible]’ => [
‘*’ => true,
‘id’ => false
],
‘[dirty]’ => [
‘number’ => true,
‘type’ => true
],
‘[original]’ => ,
‘[virtual]’ => ,
‘[errors]’ => [
‘user_id’ => [
‘_required’ => ‘This field is required’
]
],
‘[invalid]’ => ,
‘[repository]’ => ‘PhoneNumbers’

    }
],

so to my understanding the problem seems to be with the foreign key, but I dont understand how to fix it?
By now this easy thing is giving me a headache, so any advice is welcomed.

P.S.: What are actually the differences between patchEntity() and save() ?

any help or a hint is very much appreciated.

You have keyed the inputs for the phone numbers incorrectly.
See this section in the Cake book for how to create a form that includes hasMany objects: https://book.cakephp.org/3.0/en/views/helpers/form.html#creating-inputs-for-associated-data

As your User hasMany PhoneNumbers these PhoneNumbers will need to be in an array. So your code should look more like:

<?php
     echo $this->Form->control('phone_numbers.0.number');
     echo $this->Form->control('phone_numbers.0.type');
?>

You could also allow create more inputs so you can add a User with more than one phone number:

<?php
     echo $this->Form->control('phone_numbers.0.number');
     echo $this->Form->control('phone_numbers.0.type');
     echo $this->Form->control('phone_numbers.1.number');
     echo $this->Form->control('phone_numbers.1.type');
     echo $this->Form->control('phone_numbers.2.number');
     echo $this->Form->control('phone_numbers.2.type');
?>

The difference between patchEntity() and save() is that you use patchEntity() to start building up an entity. This entity will only exist in your computer’s memory until you successfully use save() to persist it to the database.

Hi annabel,

first of all thank u very much for ur reply. I had tried it the way u suggested and as it is also documented in the cookbook. But it doesnt make any difference. Debugging it, the error is still the same:

[[errors]] => Array
(
[user_id] => Array
(
[_required] => This field is required
)

                    )

Yesterday, I came across a similar question on github from 2014

I tried to do it the same way with hidden inputs, but received a saved error. But Im also new to Cakephp and not very much familiar with hidden inputs (just read about it in the documentation), so maybe I handled it the wrong way. Do u know more about hidden inputs?

solved the problem with hidden inputs. Probably this isnt an elegant way, but at least it works now.

Hello, could you please explainme how did you solve this issue, I’m truing to do that so…

if u trying to save for new record(s) u can follow to what annabel sugested,

but if u trying to update/edit record(s) along with associated data.

since the id field is protected by default inside the entity rule, (u can check the related model entity)
so you need to make the ids of associated data accessible.

example :

$this->Users->patchEntity($entity, $this->request->getData, [
‘associated’ => [
‘UserProfiles’ => [‘accessibleFields’ => [‘id’ => true]
]
]
]);

by this you don’t need hidden field for defining the IDs.

sorry for my bad english, hope this might help.

I think this link can be helpful for cakephp3. you can crud accordingly.

[creating-inputs-for-associated-data]

Use Entities and save with foreach…