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
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:
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() ?
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.
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?
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.