Relationship Question ORM Kohana

I have tables:

  users {id, name}
 projects {id, name}
 roles {id, name}
 projects_users {id, user_id, project_id, role_id}

I have models:

  project {has many users through projects_users}
 user {has many projects through projects_users}

Question : How can I get user roles for one project? Or maybe I need to restore tables?

the code:

  $ project = ORM :: factory ('project', $ id);
 $ users = $ project-> users-> find_all ();
 foreach ($ users as $ u) {
     $ roles = $ u-> .... How to get all roles for this user and for this project?
 }
+4
source share
1 answer

Your project_users table, apparently, represents roles in projects, adds another model linked to this table:

project_role { has one user has one role has one project } user { has many project_role ... } project { has many project_role ... } 

Then you could do:

 $user = ORM::factory('user') ->with('project_role') ->where('project_role.project_id', '=', $id) ->with('project_role:role')->findall(); 

If this does not work, one of the following should work, but may be another form of workaround for what you need.

 $project = ORM::factory('project', $id); $roles = $project->project_role->with('user')->with('role')->findall(); 

or

 $roles = ORM::factory('project_role') ->where('project_id', '=', $id) ->with('user')->with('role')->findall(); 
+1
source

All Articles