sql - 有没有办法优化用户的映射?
问题描述
我创建了一个方法来调用这个 getUserIdsByAllBranches() 并且返回将是一个数组。如果数组不为空,我将进行另一个过程。但是我在映射或获取所有分支的所有用户时遇到了问题。
这是代码:
/**
* Get all User Ids of Branch Permission User
* By All Branches
* @param Array
* @return Array
*/
public function getUserIdsByAllBranches($override = [])
{
if (count($override) !== 0) {
foreach($override as $property => $value) {
${$property} = $value;
}
} else {
$branchLength = App::make(BranchRepository::class)->get()->count();
}
$userIdsWithAllBranches = [];
$userIds = $this->getUserIds();
foreach($userIds as $userId) {
$identical = true;
$userBranchIds = array_values($this->get()
->where('user_id', $userId)
->pluck(['branch_id'])
->unique()
->toArray());
if(count($userBranchIds) === $branchLength) {
$initialPermissions = [];
foreach($userBranchIds as $index => $userBranchId) {
if(!$identical) {
continue;
}
$branchUserPermissions = $this->get()
->where('user_id', $userId)
->where('branch_id', $userBranchId)
->sortBy('permission_id')
->pluck(['permission_id']);
if($index === 0) {
$initialPermissions = $branchUserPermissions;
} else {
if($initialPermissions != $branchUserPermissions) {
$identical = false;
}
}
}
} else {
$identical = false;
}
if($identical) {
array_push($userIdsWithAllBranches, $userId);
}
}
return $userIdsWithAllBranches;
}
/**
* Get all User Ids of Branch Permission User
*
* @return Array
*/
public function getUserIds()
{
return array_values($this->get()
->sortBy('user_id')
->pluck(['user_id'])
->unique()
->toArray());
}
此方法 getUserIds() 将返回分支权限用户表的所有用户 ID。
分支权限用户表:
- branch_id
- 许可ID
- 用户身份
解决方案
推荐阅读
- javascript - 将值 date.now() "JS" 转换为日期格式 "MM/dd/yyyy HH:mm:ss"
- sql - 将 0019 年更改为 2019
- python - 文件未正确到达
- python - 另一个线程在等待 HTTP 响应时会执行另一个线程吗?
- jquery - jquery重置数组中的所有输入字段
- c++ - 选择排序时交换函数调用的次数和完成的交换次数是否相同?
- android - 地图类型 GoogleMap.MAP_TYPE_NONE 的 Google 地图背景颜色
- c++ - cudaMalloc() 是否将数组初始化为 0?
- swift - 如何在主线程(Swift)中等待异步 Firebase 调用
- python - 为什么我不能显示加载的腌制深度学习模型的分数?