Kohana ORM 关系问题

发布于 2024-09-12 21:21:41 字数 535 浏览 7 评论 0原文

我有桌子:

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

我有模型:

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

问题: 如何获取一个项目的用户角色?或者也许我必须重建我的桌子?

代码:

$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?
}

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?
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

許願樹丅啲祈禱 2024-09-19 21:21:41

您的project_users表似乎代表项目中的角色,添加与该表相关的另一个模型:

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

然后您可以执行以下操作:

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

如果这不起作用,则以下之一应该可以工作,但可能是另一种形式遍历到你想要的东西。

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

或者

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

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文