Model hasManyThroughMany relationships

Posted 3 years ago by rleger

Hi everyone,

I'm trying to figure out a model relationship issue. I'm building an app that needs to record ambulance interventions.

An intervention hasMany Patients (and a patient can belong to many interventions) A patient hasMany Diagnostics (and a diagnostic can belong to many patients)

So a patient may be involved in different interventions, I must be able to retrieve patient diagnostics (all the diagnostics regardless of which intervention it happened in) but also - and that's my problem - only the diagnostics for that patient on that intervention.

My pivot table are defined as followed :




right now I have a many to many relationship between interventions <-> patients and between patients <-> diagnostics

I made it work this way : my patient model has a diagnostics method that checks wether I'm calling from the patient model (I want all diagnostics regardless of the intervention) or if I'm calling from an intervention in which case I should only get the diagnostics for that patient on that intervention by using an ugly where method. I do this by checking if the $this->pivot is set and retrieve the intervention_id from it... Hacky but it works except when I use it with Fractal/Dingo to get nested relationships. If I want to get the interventions and include patients and patients.diagnostics it will spit out all the diagnostics for a patient regardless of the current intervention. Basically when it hits my patient model it fails to see I'm calling 'from' my intervention model because there is no Pivot table.

I need to represent (InterventionPatient) as en entity, I event toyed around with defining it as a model but it created more problems than it solved.

I've been struggling with this for a bit but I can't find a way. I can't use hasManyThrough because they're both many to many relationships.

Basically I need hasManyThroughMany .. :)

I thought doctrine might have what I need... lets hope not, I really don't want to rewrite everything :)

Thanks for your inputs

Please sign in or create an account to participate in this conversation.