我正在尝试提供一份集合的副本users
到一个雄辩的模型jobs
。所以我实际上有:
jobs : [
1 : {
users : {
1: {}
2: {}
3: {}
}
}
2 : {
users : {
1: {}
2: {}
3: {}
}
}
]
一旦我得到这个,我将对另一个查询中的一些数字进行求和,本质上为每个作业的每个用户提供一个总数,所以上面的结果可能看起来像这样:
jobs : [
1 : {
users : {
1: {
total: 120
}
2: {
total: 45
}
3: {
total: 12
}
}
}
2 : {
users : {
1: {
total: 32
}
2: {
total: 4
}
3: {
total: 17
}
}
}
]
除了我似乎无法克隆这个用户列表,而且我实际上最终得到的所有总数彼此相同:
{
1:{
id:1,
users:{
1:{
id:1,
total:807
},
2:{
id:2,
total:9
}
}
},
2:{
id:2,
users:{
1:{
id:1,
total:807
},
2:{
id:2,
total:9
}
}
},
3:{
id:3,
users:{
1:{
id:1,
total:807
},
2:{
id:2,
total:9
}
}
}
}
这是我正在做的事情:
public function summary()
{
$jobs = Job::all()->keyBy('id');
$users = User::with('closed_time_chunks')->get();
$users_list = User::all(['id'])->keyBy('id');
// I think this is the problem statement:
foreach ($jobs as $job):
$job->users = clone $users_list;
endforeach;
Log::info('Starting');
foreach ($users as $user):
foreach ($user->closed_time_chunks as $chunk):
Log::info('Adding ' . ($chunk->stop_time - $chunk->start_time) . ' to job: ' . $chunk->job_id);
$jobs[$chunk->job_id]->users[$chunk->user_id]['total'] += $chunk->stop_time - $chunk->start_time;
endforeach;
endforeach;
}
我的猜测是,我实际上只是创建对同一事物的引用,任何添加实际上只是添加到“主”集合中。如何成功克隆用户以使总数在作业中是唯一的?
Edit
使用数组(正如 Matheos 推荐的那样)会导致一个非常奇怪的错误:
错误异常(E_NOTICE)
间接修改重载属性 Job::$users 没有效果