diff --git a/eZ/Bundle/EzPublishCoreBundle/Resources/config/commands.yml b/eZ/Bundle/EzPublishCoreBundle/Resources/config/commands.yml index 29a273a993..33564589cb 100644 --- a/eZ/Bundle/EzPublishCoreBundle/Resources/config/commands.yml +++ b/eZ/Bundle/EzPublishCoreBundle/Resources/config/commands.yml @@ -70,3 +70,11 @@ services: $connection: '@ezpublish.persistence.connection' tags: - { name: console.command } + + Ibexa\Bundle\Core\Command\DuplicateFieldsCommand: + autowire: true + autoconfigure: true + arguments: + $connection: '@ezpublish.persistence.connection' + tags: + - { name: console.command } diff --git a/src/bundle/Core/Command/DuplicateFieldsCommand.php b/src/bundle/Core/Command/DuplicateFieldsCommand.php new file mode 100644 index 0000000000..14a26d22eb --- /dev/null +++ b/src/bundle/Core/Command/DuplicateFieldsCommand.php @@ -0,0 +1,84 @@ +connection = $connection; + } + + public function configure(): void + { + $this->addOption('count', null, InputOption::VALUE_REQUIRED); + $this->addOption('version-no', null, InputOption::VALUE_REQUIRED); + $this->addOption('attribute-id', null, InputOption::VALUE_REQUIRED); + $this->addOption('content-id', null, InputOption::VALUE_REQUIRED); + $this->addOption('language-id', null, InputOption::VALUE_REQUIRED); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $query = $this->connection->createQueryBuilder(); + + $query + ->select('*') + ->from('ezcontentobject_attribute') + ->andwhere('version = :version') + ->andWhere('contentobject_id = :content_id') + ->andWhere('contentclassattribute_id = :attribute_id') + ->andWhere('language_id = :language_id') + ->orderBy('id', 'desc') + ->setMaxResults(1); + + $attribute = $query->setParameters([ + 'version' => (int)$input->getOption('version-no'), + 'content_id' => (int)$input->getOption('content-id'), + 'attribute_id' => (int)$input->getOption('attribute-id'), + 'language_id' => (int)$input->getOption('language-id'), + ])->execute()->fetchAssociative(); + + if (false === $attribute) { + throw new RuntimeException('Attribute not found'); + } + + unset($attribute['id']); + + $count = (int)$input->getOption('count'); + + $query = $this->connection->createQueryBuilder(); + $query + ->insert('ezcontentobject_attribute'); + + foreach ($attribute as $key => $value) { + $query->setValue($key, ':' . $key); + $query->setParameter(':' . $key, $value); + } + + for ($i = 0; $i < $count; ++$i) { + $query->execute(); + } + + return Command::SUCCESS; + } +}