Reputation: 3823
I'm writing a cake app where I'm trying to model a situation where a group has permissions to services, and users in that group can have a subset of those permissions. If the group loses permission to a service, the users in that group should also lose permission.
My tables:
groups (id), services (id), groups_services (id, service_id, group_id), users (id, group_id), users_services (id, groups_services_id, user_id)
Currently, groups has a HABTM relationship with services. The problem is that if I remove a group from a service, then it should be removed from all users in that group as well. I know normally with CakePHP you can do this by having a dependent => true defined on your model relationship, but in this case, users_services is dependent on the join table, groups_services, that represents the HABTM, so I don't think this will work. Will I have to break up the HABTM relationship in order to get this to work, and use instead groups hasMany groups_services and services hasMany group_services?
Upvotes: 0
Views: 704
Reputation: 13952
From the cookbook:
HABTM data is treated like a complete set, each time a new data association is added the complete set of associated rows in database is dropped and created again so you will always need to pass the whole data set for saving. For an alternative to using HABTM see hasMany through (The Join Model)
So, HABTM is mainly good for pretty 'dumb' relationships.
In most situations where you want any kind of fine-grained control, HABTM isn't a good fit.
So, short answer, yes. Break up your HABTM and use a has many through, as you thought.
Upvotes: 1