I´ve a problem saving associated data. Im saving “Users” and the associated data “Groups”. The problem is when I´m saving a id of a group that not exists or the id of the group is zero, cakephp creates a new registry in the table of “Groups”. I only want to save the association if the group exists. There is any way to do this?
You can check the content of the object in the controller action before/after saving using $this->log($user) (will write to error-log). This might help you to find data-driven errors (such as wrongly named fields)
I´ve edited the code. Now it´s written in CakePHP`s syntaxis. But my problem it´s the same. I´ve check the data received and they are the expected. I think the problem is the CakePHP´s behavior, and I want that CakePHP only save the data in “GroupsUsers” and not in “Groups”.
I´ve change the code to a CakePHP´s syntaxis. I want CakePHP only save de relational data (table “GroupsUsers”), but if the group.0.id input is 0 or an id that not exists in “Groups” table CakePHP creates a new Group and a new GroupUser.
If you only want the relation to be saved in GroupsUsers, why do you specify belongsToMany-Groups instead of belongsToMany-GroupsUsers (or whatever type your relationship is) ?
Is it possible that you want to implement HABTM between Users and Groups here?
Regarding your original question:
You can e.g. check if your users-object contains a group with a valid ID (so it seems to exist) before saving and if not, do
It´s true, I should use the relation to the GroupsUsers directly, this way new groups can´t be created. The problem of to save not existing ids or 0 can be solved with unset or other code as you say, but I would like to know if CakePHP can check automaticly if the relation is valid before save.
Unfortunately, the only other way that comes to my mind would be to create validation rules in the specific tables to achieve this. You could then use a different rule-set on a “admin-method” to create groups or so.
However, I’m currently not sure if the “base”-entity will save if saving the association fails…