To frame the question, I’m a designer/front-end developer and looking for some guidance on how to set up my models. I’ve read through the Cake book on associations and HABTM and hasMany through in particular, but still don’t quite understand which is right for my needs.
I’ve built a Cake app with the following models and simple relationships:
User hasMany Workout
User hasMany Shoe
Shoe hasMany Workout
I’d like to allow users to add other users to a workout, so User HABTM Workout. Adding User2 to the workout would create the workout for User2 as well.
I also want User2 to be able to edit the details of the workout for themselves, while maintaining the association (and User1’s details). For example, User2 can specify their own notes or a time for the workout that is different than those specified by User1.
It seems like a HABTM association won’t allow for the User-specific details part, but hasMany through might? Could I do something like this:
User hasMany WorkoutDetail
Workout hasMany WorkoutDetail
User hasMany Shoe
Shoe hasMany Workout
where WorkoutDetail stores all the individual details for each workout for each user?
thanks
Yes, your second solution would work well, that is how I usually do HABTM relations when there is extra information to be stored in the join table.
One more thing you could add is
This way, if you query the
WorkoutDetailtable, you get theUserandWorkoutinformation along with it.And like you said, each user should have a seperate entry in the WorkoutDetail table.