Hallo,
ich habe aktuell folgendes Problem: Ich möchte nach Möglichkeit alle Gruppenberechtigungen und dessen Benutzer in einem Query aus der Datenbank holen. Ist das möglich?
Meine Tabellen sehen so aus:
Ohne Foreign Key Kennzeichnung. Das Skript zum Laden sieht aktuell so aus:
Nur habe ich damit das Problem, dass jede Gruppe mindestens einen Benutzer haben muss, um die Rechte der Gruppe zu erhalten. Da die Rechte nach oben, also nach Gruppen mit mehr Berechtigungen vererbt werden, kommt es vor, dass Berechtigungen fehlen. Nun kannte ich so grob die outer joins. Im Internet fand ich auch prompt den full outer join. Dieser ist jedoch in MySQL nicht implementiert. Einen Workaround dazu fand ich hier: http://stackoverflow.com/questions/2...357262#3357262
Jetzt meine Frage: Kann ich das so auf meinen Fall anwenden oder führt kein Weg an zwei Querys vorbei?
MFG derwunner
ich habe aktuell folgendes Problem: Ich möchte nach Möglichkeit alle Gruppenberechtigungen und dessen Benutzer in einem Query aus der Datenbank holen. Ist das möglich?
Meine Tabellen sehen so aus:
Code:
create table profile ( id int not null auto_increment, email char(50) not null, password char(100) not null, name char(100) not null, surname char(50) not null, player_name char(50) not null, street char(50) not null, homeno char(5) not null, postalcode char(5), homearea char(100) not null, group_id int not null, primary key(id) ); create table usergroups ( id int auto_increment not null, name char(30) not null, primary key(id) ); create table actions ( id int not null auto_increment, name char(50) not null, primary key(id) ); create table group_rights ( id int auto_increment not null, action_id int not null, group_id int not null, allowed int(1) not null, primary key(id) );
PHP-Code:
[...]
public function load()
{
$sSQL = <<<SQL
select `usergroups`.`name` as `group`, `actions`.`name` as `action`, `profile`.`email` as `user`, `allowed`
from profile
inner join usergroups on usergroups.id = profile.group_id
inner join group_rights on usergroups.id = group_rights.group_id
inner join actions on group_rights.action_id = actions.id
order by profile.group_id;
SQL;
$arRightList = array();
$pdo = $this->_db->getPdo();
$stmt = $pdo->prepare($sSQL, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
$stmt->execute();
$arRow = $stmt->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT);
$auth = new AuthList();
if ($arRow)
{
$sGroup = $arRow['group'];
$sGroupNew = $sGroup;
$arActRights = array();
$arActUsers = array();
}
while ($arRow)
{
while ($sGroup === $sGroupNew && $arRow)
{
if ($arRow['allowed'] == 0)
{
$arActRights[$arRow['action']] = AuthList::ACTION_DENY;
}
else
{
$arActRights[$arRow['action']] = AuthList::ACTION_ACCEPT;
}
if (!in_array($arRow['user'], $arActUsers) && $sGroup === $sGroupNew)
{
$arActUsers[] = $arRow['user'];
}
// read after and set new group
$arRow = $stmt->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT);
$sGroupNew = $arRow['group'];
}
$auth->addGroup($sGroup, $arActRights);
foreach ($arActUsers as $sActUser)
{
$auth->setPrimaryUserGroup($sGroup, $sActUser);
}
$arActRights = array();
$arActUsers = array();
$sGroup = $sGroupNew;
}
$stmt->closeCursor();
return $auth;
}
Jetzt meine Frage: Kann ich das so auf meinen Fall anwenden oder führt kein Weg an zwei Querys vorbei?
MFG derwunner
Kommentar