Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Nested Models relationships


Has anyone ever dealt with "nested" models and if so how do you go about it?

Consider that I have the following 3 models

User Project ProjectUser

Now "User" is just a user of the application. They come from a 3rd party login, so although there is a "User" model, there isn't actually a database table for it.

"Project" is basically what it says on the tin. It's a table in the database that might hold info about a project such as a title an id and an end_date

The "ProjectUser" table holds relationships between a "User"->id and a "Project"->id again this is a physical table in the database

Now I know in my "User" model in the initialize method I can define a relationship of $this->hasMany("uid", "ProjectUser", "uid", NULL);

and then calling User->ProjectUser returns me everything I need. However the end game here would actually be to get all the "Project" models that a User is associated via ProjectUser. I know I could loop through each User->ProjectUser to get the Project->id but I wondered if there is a simpler method of doing this by defining relationships? Or should I just drop back to phql? Or is my model structure wrong here?

I would be interested in any opinions



I managed to figure this one out. I needed a many-to-many relationship such as

$this->hasManyToMany( "uid", "ProjectUser", "uid", "project_id", "Project", "project_id" );