我使用此函数返回具有角色和组的用户。
我想返回具有正确索引的对象数组,这就是我创建$ index counter的原因。
问题在于,如果一个用户拥有超过1个组,我会获得重复用户。
因此,例如,如果我有一个拥有3个组的用户,我将获得该用户3次。
如何避免重复用户?
我知道我需要检查那个用户是否已经存在,比如我检查了角色,但我不确定在哪里。
如果我将用户[' $ index']替换为用户[' $ id'],此代码将有效,但这样我就无法获得具有正确索引的数组,这就是我需要的。
$stmt = $mysqli->prepare("
SELECT u.id
, u.firstName
, u.lastName
, u.email
, u.phoneNumber
, u.address
, u.birthDate
, ur.roleName
, cg.id
, cg.name
FROM users as u
LEFT
JOIN user_role as ur
ON u.id = ur.userId
LEFT
JOIN user_group as ug
on ug.userId = u.id
LEFT
JOIN control_group as cg
on cg.id = ug.groupId
WHERE u.id != ?");
$stmt->bind_param("i", $_SESSION["id"]);
$stmt->execute();
$stmt->bind_result($id, $firstName, $lastName, $email, $phoneNumber,
$address, $birthDate, $roleName, $groupId, $groupName);
$users = array();
$index = 0;
while ($stmt->fetch()) {
if (empty($users[$index])) {
$users[$index] = array(
'id' => $id,
'firstName' => $firstName,
'lastName' => $lastName,
'email' => $email,
'phoneNumber' => $phoneNumber,
'address' => $address,
'birthDate' => $birthDate,
'roles' => array(),
'groups' => array()
);
}
if ($roleName) {
$found = false;
foreach ($users[$index]['roles'] as $role) {
if($role['roleName'] == $roleName){
$found = true;
break;
}
}
if($found == false)
$users[$index]['roles'][] = array(
'roleName' => $roleName
);
}
if ($groupId) {
$found = false;
foreach ($users[$index]['groups'] as $group) {
if($group['groupName'] == $groupName){
$found = true;
break;
}
}
if($found == false)
$users[$index]['groups'][] = array(
'groupName' => $groupName
);
}
$index++;
}
$stmt->close();
$mysqli->close();
echo json_encode($users);
所以基本上我期待像这样的对象
{
"id":2,
"firstName":"Jon",
"lastName":"Doe",
"email":"jon.doe@email.com",
"phoneNumber":"0621-123-444",
"address":"Address 12a",
"birthDate":"1976-01-01",
"roles":['list of role objects'],
"groups":['list of group objects']
}
此外,我不确定我是否正确地生成对象,我想如果有人能告诉我什么是正确的方法以及如何正确生成像这样的对象。
答案 0 :(得分:1)
如果执行多个查询是一个选项,您可以首先获得所有用户,然后每个用户获得角色和组。
$user_stmt = $mysqli->prepare("SELECT id, firstName, lastName, email, phoneNumber, address, birthDate FROM users WHERE id != ?");
$user_stmt->bind_param("i", $_SESSION["id"]);
$user_stmt->execute();
$user_stmt->bind_result($id, $firstName, $lastName, $email, $phoneNumber, $address, $birthDate);
$role_stmt = $mysqli->prepare("SELECT roleName FROM user_role WHERE userId = ?");
$group_stmt = ...
$users = array();
while($user_stmt->fetch())
{
$role_stmt->bind_param("i", $id);
$role_stmt->execute();
$role_stmt->bind_result($roleName);
$roles = array();
while($role_stmt->fetch())
{
$roles[] = array("roleName" => $roleName);
}
$groups = array();
// Same as for roles
$users[] = array(
'id' => $id,
'firstName' => $firstName,
'lastName' => $lastName,
'email' => $email,
'phoneNumber' => $phoneNumber,
'address' => $address,
'birthDate' => $birthDate,
'roles' => $roles,
'groups' => $groups
);
}
}
$user_stmt->close();
$role_stmt->close();
$group_stmt->close();
$mysqli->close();
echo json_encode($users);
答案 1 :(得分:0)
在我看来,一旦你使用连接获得用户和数据库组,你就会为同一个用户提供多个索引,而你却无法改变这一事实。
所以,你有两个选择。 - 您可以使用user_id并统一数组中的组和角色 - 或者您可以在索引位置使用user_id,并为用户构建索引列表,方法与使用角色列表相同
但是如果你真的需要知道你在数据库中获得的每个用户的索引,你只需要这样做。
我需要了解上下文或用户故事以获得更准确的想法
好的,我尝试这样的事情:
$index = 0;
$temp_index = 0;
$user_index =[]
while ($stmt->fetch()) {
if (empty($user_index[$id])){
$user_index[$id] = $index;
}else{
$temp_index = $index;
$index = $user_index[$id];
}
if (empty($users[$index])) {
$users[$index] = array(
'id' => $id,
'firstName' => $firstName,
'lastName' => $lastName,
'email' => $email,
'phoneNumber' => $phoneNumber,
'address' => $address,
'birthDate' => $birthDate,
'roles' => array(),
'groups' => array()
);
}
if ($roleName) {
$found = false;
foreach ($users[$index]['roles'] as $role) {
if($role['roleName'] == $roleName){
$found = true;
break;
}
}
if($found == false)
$users[$index]['roles'][] = array(
'roleName' => $roleName
);
}
if ($groupId) {
$found = false;
foreach ($users[$index]['groups'] as $group) {
if($group['groupName'] == $groupName){
$found = true;
break;
}
}
if($found == false)
$users[$index]['groups'][] = array(
'groupName' => $groupName
);
}
if ($tempo_index != 0){
$index = $temp_index;
$temp_index = 0;
}else{
$index ++;
$temp_index = 0;
}