I’m trying to create an application which should help distribute work to employees or volunteers on a irregular time schedule based on their availabilities.
Anyway, here are my models and the relations between them :
Users hasMany Jobs
Jobs belongTo Users
Users hasMany Availabilities
Availabilities belongTo Users
Jobs hasOne Periods
Periods belongTo Jobs
Availabilities hasOne Periods
Periods belongTo Availabilities
The problem is that “Periods” is on the receiving side of two hasOne relationships and, if I’m not wrong, it’s something you can’t do in CakePHP. What is the best way to proceed in this situation?
You might have also spotted the fact a “Job” may be assigned to someone (a “User”) or not. Should I drop the relationship and create it once the job has been assigned or should I create a fictional user representing “nobody” for unassigned jobs?
I would have posted a nice image but i don’t have enough reputation, sorry!
Here’s a link though.
I don’t see a problem. You should be able to add the foreign key for each hasOne relationship to the
periodstable (periods.job_idandperiods.availability_id).This is fine. Just set the
jobs.user_idto allow null values and set up the relationship. This will allow jobs to be created without being assigned to users. After you perform a find you can checkempty($results['User'])to determine if you are dealing with an unassigned job.Just a reminder: model names should be singular (ie. User hasMany Job, Job belongsTo User, etc.)
You can write validation for that in the Period model: