From 5e3851a6ca7bfff1fa753a5497f565f19df4e7d6 Mon Sep 17 00:00:00 2001 From: b1ek Date: Tue, 8 Oct 2024 15:06:22 +1000 Subject: [PATCH] fix: rewrite app:update-all-users-cron --- src/Command/UpdateAllUsersCronCommand.php | 47 ++++++----------------- 1 file changed, 12 insertions(+), 35 deletions(-) diff --git a/src/Command/UpdateAllUsersCronCommand.php b/src/Command/UpdateAllUsersCronCommand.php index a39e23d..a51e16b 100644 --- a/src/Command/UpdateAllUsersCronCommand.php +++ b/src/Command/UpdateAllUsersCronCommand.php @@ -31,45 +31,22 @@ class UpdateAllUsersCronCommand extends Command protected function execute(InputInterface $input, OutputInterface $output): int { - $io = new SymfonyStyle($input, $output); - $em = $this->entityManager; $repo = $em->getRepository(User::class); - $chunkSize = 100; - $count = $repo->count([]); + $repo->createQueryBuilder('u') + ->update() + ->set('u.log', 0) + ->where('u.log = 1') + ->getQuery() + ->execute(); - $io->progressStart($count); - for ($i = 0; $i < $count; $i += $chunkSize) { - $data = $repo->findBy([], null, $chunkSize, $i); - if ($data == []) { - break; - } - - /** @var User */ - foreach ($data as $user) { - $score = (int) $user->getScore(); - if ($score < 100 || $score > 900) { - $user->setLog(true); - - $log = new UserLog(); - $log->setScore($user->getScore()); - $log->setUserId($user->getId()); - - $em->persist($user); - $em->persist($log); - } else if ($user->getLog()) { - $user->setLog(false); - $em->persist($user); - } - } - - $io->progressAdvance($chunkSize); - - $em->flush(); - $em->clear(); - } - $io->progressFinish(); + $repo->createQueryBuilder('u') + ->update() + ->set('u.log', 1) + ->where('u.score > 900 OR u.score < 100') + ->getQuery() + ->execute(); return Command::SUCCESS; }