English 中文(简体)
Kohana ORM relationships question
原标题:

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 i get user roles for one project? Or maybe i have to reconstruct my tables?

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?
}
最佳回答

Your project_users table seems to be representing roles on projects, add another model which is tied to that table:

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

Then you might be able to do:

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

If that doesn t work, one of the following should work, but may be a different form of traversal to what you re after.

$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();
问题回答

暂无回答




相关问题
Kohana - subfolders within views folder

I m working on the admin section of a site using Kohana. I ve created a "admin" subfolder within the views folder to store admin views. I m also using a modified instance of the Template Controller ...

approach for "site down for maintenance"

I have been using Joomla and I love its administrative facility to put the site down for maintenance. As I have seen, all requests to the site if it is in maintenance mode is routed to a single page. ...

Kohana 3: using maintainable routes

I m using Kohana v3 for a web project, and today I found myself writing this: echo Html::anchor( user/view/ .$user->id, "See user s profile"); If I rename the action_view method in the User ...

Is using a Mail Model in MVC incorrect?

I have built a model in some of my MVC websites to assist with sending emails, generally I do something like this $mail = new Mail_Model; $mail->to( me@somewhere.com ); $mail->from( you@...

help on building a basic php search engine

i looked for tutorials everywhere but just can t seem to get a good one... a search page with pagination, column header sorting, and multiple filtering(filters are in checkboxes) the problem: had ...

What s the proper MVC way to do this....?

Quick question about general MVC design principle in PHP, using CodeIgniter or Kohana (I m actually using Kohana). I m new to MVC and don t want to get this wrong... so I m wondering if i have ...

Kohana Error... Attempt to assign property of non-object

So I m trying to go through the Version 3 Guide of Kohana and keep getting an error on the hello world create view part. ErrorException [ Warning ]: Attempt to assign property of non-object Line 8: $...

热门标签