我想通过使用控制台/命令和chunkbyid将获得数百万行的审计表记录移动到另一个表中,但它停留在中间。例如,我想移动审核月的日期(Created_at)= 02和年份(Created_at)= 2021,它不会运行在该条件下的所有记录中。正如我在mysql中所做的那样,它应该有大约5英里的记录,但只有几十万条记录。我在控制台中的代码如下
Audit::query()
->whereRaw("MONTH(created_at) = '$month'")
->whereRaw("YEAR(created_at) = '$year'")
->chunkById(1, function ($audits) use ($table_name) {
foreach($audits as $audit){
dump($audit->id);
$newRecord = $audit->replicate()->fill([
'audit_id' => $audit->id,
'created_at' => $audit->created_at,
'updated_at' => $audit->updated_at,
]);
$newRecord->setTable($table_name);
$newRecord->save();
if(str_contains($audit->auditable_type, 'User') || str_contains($audit->auditable_type, 'Trans') || str_contains($audit->auditable_type, 'Step')|| str_contains($audit->auditable_type, 'Team')){
$audit->delete();
}
}
}, $column = 'id');
我已经遵循了许多解决方案,我在许多网站,但仍然没有发挥作用。我错过了什么吗?
发布于 2022-11-04 04:43:42
在Laravel (https://laravel.com/docs/9.x/queries)中有一个值得注意的块,比如When updating or deleting records inside the chunk callback, any changes to the primary key or foreign keys could affect the chunk query. This could potentially result in records not being included in the chunked results.
,在您的代码中,您在某些情况下删除了审计。
https://stackoverflow.com/questions/74312112
复制相似问题