I'm working on an application for iceskaters with cakephp. A part of my database shema is like this :
Nations-(1,n)-Skaters-(1,n)-Participations-(n,1)-Competitions
Participations has a rank field and Competitions has a date field
I want to have an hasOne association from Skaters to the last Participation (of this skater) according to the date field in the table Competitions. I need that in model because i want to order Skaters by the last Participation or by the rank obtained during the last participation.
I don't know if it is possible with cakephp v3.x
In MySQL i write a query that seems to works :
Select s.id, c.title, c.date, p.id, p.rank
from
(
SELECT skater_id, max(competitions.date) as maxdate
FROM `participations`
inner join competitions on competitions.id = participations.competition_id
group by skater_id
) as x
inner join participations as p on p.skater_id = x.skater_id
inner join competitions as c on c.id = p.competition_id
inner join skaters as s on x.skater_id = s.id and maxdate = c.date
I've found a very usefull post but i'm not able to adapt it to my case (filtering field in third table not second one). The only result was : An Internal Server Error Occurred. So too dificult for me to debug without information.
Regards