Flatten associated records into "top level"

Clients hasMany Project
Project hasMany Tasks

I want to show

Client A      Project A      Task 1
Client A      Project B      Task 1
Client B      Project A      Task 1
Client C      Project A      Task 1
Client D      -                    -
Client E      -                    -

If the client doesn’t have a project, I still want to show the client - this is easily achieved by find('all') and a LEFT join on Clients.

However when the client has more than 1 project, I am not sure how to output the client row again because the associated entities are nested

Client ..

Does this make sense - I looked at the hydrate option but don’t really understand it enough.


When you say “want to show”, does that mean in some HTML you are generating, or in an API response? If the former, it seems that you have the data you need, and just need to adjust your output generation. Iterate over each client, and within that you iterate over each project for that client, and within that you iterate over each task for that project. In that task loop, you still have access to the project and the client variable, so you can output them in each row.