Kohana ORM relazioni domanda
-
25-09-2019 - |
Domanda
Ho tabelle:
users {id, name} projects {id, name} roles {id, name} projects_users {id, user_id, project_id, role_id}
Non ho modelli:
project { has many users through projects_users } user { has many projects through projects_users }
Domanda : Come ottengo ruoli utente per un progetto? O forse devo ricostruire le mie tabelle?
Codice:
$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? }
Soluzione
La tabella project_users sembra rappresentare ruoli in progetti, aggiungere un altro modello che è legato a quel tavolo:
project_role {
has one user
has one role
has one project
}
user {
has many project_role
...
}
project {
has many project_role
...
}
Poi si potrebbe essere in grado di fare:
$user = ORM::factory('user')
->with('project_role')
->where('project_role.project_id', '=', $id)
->with('project_role:role')->findall();
Se questo non funziona, una delle seguenti dovrebbe funzionare, ma può essere una diversa forma di attraversamento a ciò che stai cercando.
$project = ORM::factory('project', $id);
$roles = $project->project_role->with('user')->with('role')->findall();
o
$roles = ORM::factory('project_role')
->where('project_id', '=', $id)
->with('user')->with('role')->findall();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow