diff --git a/application/Espo/Classes/ConsoleCommands/RebuildCategoryPaths.php b/application/Espo/Classes/ConsoleCommands/RebuildCategoryPaths.php new file mode 100644 index 0000000000..89d8ffb060 --- /dev/null +++ b/application/Espo/Classes/ConsoleCommands/RebuildCategoryPaths.php @@ -0,0 +1,70 @@ +rebuildPaths = $rebuildPaths; + } + + public function run(Params $params, IO $io): void + { + $entityType = $params->getArgument(0); + + if (!$entityType) { + $io->setExitStatus(1); + $io->writeLine("Error: No entity type. Should be specified as the first argument."); + + return; + } + + try { + $this->rebuildPaths->run($entityType); + } + catch (Exception $e) { + $io->setExitStatus(1); + $io->writeLine("Error: " . $e->getMessage()); + + return; + } + + $io->writeLine("Done."); + } +} diff --git a/application/Espo/Resources/metadata/app/consoleCommands.json b/application/Espo/Resources/metadata/app/consoleCommands.json index c9d52ecb2c..019d9059a0 100644 --- a/application/Espo/Resources/metadata/app/consoleCommands.json +++ b/application/Espo/Resources/metadata/app/consoleCommands.json @@ -26,5 +26,9 @@ }, "version": { "listed": true + }, + "rebuildCategoryPaths": { + "className": "Espo\\Classes\\ConsoleCommands\\RebuildCategoryPaths", + "listed": true } } diff --git a/application/Espo/Tools/CategoryTree/RebuildPaths.php b/application/Espo/Tools/CategoryTree/RebuildPaths.php new file mode 100644 index 0000000000..83aead840e --- /dev/null +++ b/application/Espo/Tools/CategoryTree/RebuildPaths.php @@ -0,0 +1,132 @@ +entityManager = $entityManager; + } + + /** + * @throws Error + */ + public function run(string $entityType): void + { + if ( + !$this->entityManager->hasRepository($entityType) || + !$this->entityManager->getRepository($entityType) instanceof CategoryTree + ) { + throw new Error("Bad entity type."); + } + + $this->clearTable($entityType); + + $this->processBranch($entityType, null); + } + + private function clearTable(string $entityType): void + { + $query = $this->entityManager + ->getQueryBuilder() + ->delete() + ->from($entityType . 'Path') + ->build(); + + $this->entityManager->getQueryExecutor()->execute($query); + } + + private function processBranch(string $entityType, ?string $parentId): void + { + $collection = $this->entityManager + ->getRDBRepository($entityType) + ->sth() + ->where(['parentId' => $parentId]) + ->find(); + + foreach ($collection as $entity) { + $this->processEntity($entity); + } + } + + private function processEntity(Entity $entity): void + { + $parentId = $entity->get('parentId'); + $pathEntityType = $entity->getEntityType() . 'Path'; + + if ($parentId) { + $subSelect1 = $this->entityManager + ->getQueryBuilder() + ->select() + ->from($pathEntityType) + ->select(['ascendorId', "'" . $entity->getId() . "'"]) + ->where([ + 'descendorId' => $parentId, + ]) + ->build(); + + $insert = $this->entityManager + ->getQueryBuilder() + ->insert() + ->into($pathEntityType) + ->columns(['ascendorId', 'descendorId']) + ->valuesQuery($subSelect1) + ->build(); + + $this->entityManager->getQueryExecutor()->execute($insert); + } + + $insert = $this->entityManager + ->getQueryBuilder() + ->insert() + ->into($pathEntityType) + ->columns(['ascendorId', 'descendorId']) + ->values([ + 'ascendorId' => $entity->getId(), + 'descendorId' => $entity->getId(), + ]) + ->build(); + + $this->entityManager->getQueryExecutor()->execute($insert); + + $this->processBranch($entity->getEntityType(), $entity->getId()); + } +}