diff --git a/application/Espo/Classes/ConsoleCommands/PopulateArrayValues.php b/application/Espo/Classes/ConsoleCommands/PopulateArrayValues.php new file mode 100644 index 0000000000..73e80e275c --- /dev/null +++ b/application/Espo/Classes/ConsoleCommands/PopulateArrayValues.php @@ -0,0 +1,108 @@ +entityManager = $entityManager; + } + + /** + * @throws Error + */ + public function run(Params $params, IO $io): void + { + $entityType = $params->getArgument(0); + $field = $params->getArgument(1); + + if (!$entityType || !$field) { + throw new Error("Entity type and field should be passed as arguments."); + } + + if (!$this->entityManager->hasRepository($entityType)) { + throw new Error("Bad entity type."); + } + + $defs = $this->entityManager->getDefs()->getEntity($entityType); + + if (!$defs->hasAttribute($field)) { + throw new Error("Bad field."); + } + + if ($defs->getAttribute($field)->getType() !== Entity::JSON_ARRAY) { + throw new Error("Non-array field."); + } + + if ($defs->getAttribute($field)->isNotStorable()) { + throw new Error("Not-storable field."); + } + + if (!$defs->getAttribute($field)->getParam('storeArrayValues')) { + throw new Error("Array values disabled for the field.."); + } + + $collection = $this->entityManager + ->getRDBRepository($entityType) + ->sth() + ->find(); + + /** @var ArrayValueRepository $repository */ + $repository = $this->entityManager->getRepository(ArrayValue::ENTITY_TYPE); + + foreach ($collection as $i => $entity) { + if (!$entity instanceof CoreEntity) { + throw new Error(); + } + + $repository->storeEntityAttribute($entity, $field); + + if ($i % 1000 === 0) { + $io->write('.'); + } + } + + $io->writeLine(''); + $io->writeLine('Done.'); + } +} diff --git a/application/Espo/Core/Console/CommandManager.php b/application/Espo/Core/Console/CommandManager.php index 767f780d78..51c3372806 100644 --- a/application/Espo/Core/Console/CommandManager.php +++ b/application/Espo/Core/Console/CommandManager.php @@ -42,11 +42,9 @@ use Espo\Core\Console\Command\Params; class CommandManager { private InjectableFactory $injectableFactory; - private Metadata $metadata; private const DEFAULT_COMMAND = 'Help'; - private const DEFAULT_COMMAND_FLAG = 'help'; public function __construct(InjectableFactory $injectableFactory, Metadata $metadata) diff --git a/application/Espo/Resources/metadata/app/consoleCommands.json b/application/Espo/Resources/metadata/app/consoleCommands.json index 019d9059a0..2216ac671f 100644 --- a/application/Espo/Resources/metadata/app/consoleCommands.json +++ b/application/Espo/Resources/metadata/app/consoleCommands.json @@ -30,5 +30,9 @@ "rebuildCategoryPaths": { "className": "Espo\\Classes\\ConsoleCommands\\RebuildCategoryPaths", "listed": true + }, + "populateArrayValues": { + "className": "Espo\\Classes\\ConsoleCommands\\PopulateArrayValues", + "listed": false } }