首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >laravel chunkById卡在中间,我是不是漏掉了什么?

laravel chunkById卡在中间,我是不是漏掉了什么?
EN

Stack Overflow用户
提问于 2022-11-04 04:13:43
回答 1查看 61关注 0票数 0

我想通过使用控制台/命令和chunkbyid将获得数百万行的审计表记录移动到另一个表中,但它停留在中间。例如,我想移动审核月的日期(Created_at)= 02和年份(Created_at)= 2021,它不会运行在该条件下的所有记录中。正如我在mysql中所做的那样,它应该有大约5英里的记录,但只有几十万条记录。我在控制台中的代码如下

代码语言:javascript
运行
复制
        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');

我已经遵循了许多解决方案,我在许多网站,但仍然没有发挥作用。我错过了什么吗?

EN

回答 1

Stack Overflow用户

发布于 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.,在您的代码中,您在某些情况下删除了审计。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74312112

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档