I seem to have this recurring problem with saving associated data.
This time, I’m experiencing the inverse of the problem I described here: Devil of a time saving relation to associated data on create
In this scenario, I’m trying to save data for an entity that the main entity belongsTo. My form contains fields for both the main entity as well as the associated entity that it belongsTo. When I save, the controller is all giddy about creating a new entity when the id doesn’t exist. I don’t even need to use newEntity()! It just does it.
But woe betide me if I try to save changes to an existing entity, It just silently fails. It seems the only way to get it to save the associated entity is to explicitly make a separate $this->Main->Assoc->save($assoc) call for that entity.
–
What made this particularly thorny for me is that I had a typo in my form and the id of the associated entity was not submitting. I NEVER realized there was a problem because Cake was happily creating a brand new entity and overwriting the old foreign key with the new one. From the user perspective, everything was working hunky dory because the data got saved and associated. But a couple months later, when I happened to be looking in the database at that particular entity, I saw dozens and dozens of duplicate entries because every time a user submitted that form with a new edit, a brand new entity was created.
When I fixed my typo, it stopped saving the associated entity information. Wow. So to save an associated entity on create, no problem. But to save it on edit, you have to jump through an extra hoop.