Skip to content

Commit 46e0e4a

Browse files
committed
[DO NOT MERGE] Duplicate fields command
1 parent 4f54a42 commit 46e0e4a

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

eZ/Bundle/EzPublishCoreBundle/Resources/config/commands.yml

+8
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,11 @@ services:
7070
$connection: '@ezpublish.persistence.connection'
7171
tags:
7272
- { name: console.command }
73+
74+
Ibexa\Bundle\Core\Command\DuplicateFieldsCommand:
75+
autowire: true
76+
autoconfigure: true
77+
arguments:
78+
$connection: '@ezpublish.persistence.connection'
79+
tags:
80+
- { name: console.command }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) Ibexa AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Ibexa\Bundle\Core\Command;
10+
11+
use Doctrine\DBAL\Connection;
12+
use RuntimeException;
13+
use Symfony\Component\Console\Command\Command;
14+
use Symfony\Component\Console\Input\InputInterface;
15+
use Symfony\Component\Console\Input\InputOption;
16+
use Symfony\Component\Console\Output\OutputInterface;
17+
18+
final class DuplicateFieldsCommand extends Command
19+
{
20+
/** @var \Doctrine\DBAL\Connection */
21+
private $connection;
22+
23+
public function __construct(
24+
Connection $connection
25+
) {
26+
parent::__construct('ibexa:content:duplicate-fields');
27+
28+
$this->connection = $connection;
29+
}
30+
31+
public function configure(): void
32+
{
33+
$this->addOption('count', null, InputOption::VALUE_REQUIRED);
34+
$this->addOption('version-no', null, InputOption::VALUE_REQUIRED);
35+
$this->addOption('attribute-id', null, InputOption::VALUE_REQUIRED);
36+
$this->addOption('content-id', null, InputOption::VALUE_REQUIRED);
37+
$this->addOption('language-id', null, InputOption::VALUE_REQUIRED);
38+
}
39+
40+
protected function execute(InputInterface $input, OutputInterface $output): int
41+
{
42+
$query = $this->connection->createQueryBuilder();
43+
44+
$query
45+
->select('*')
46+
->from('ezcontentobject_attribute')
47+
->andwhere('version = :version')
48+
->andWhere('contentobject_id = :content_id')
49+
->andWhere('contentclassattribute_id = :attribute_id')
50+
->andWhere('language_id = :language_id')
51+
->orderBy('id', 'desc')
52+
->setMaxResults(1);
53+
54+
$attribute = $query->setParameters([
55+
'version' => (int)$input->getOption('version-no'),
56+
'content_id' => (int)$input->getOption('content-id'),
57+
'attribute_id' => (int)$input->getOption('attribute-id'),
58+
'language_id' => (int)$input->getOption('language-id'),
59+
])->execute()->fetchAssociative();
60+
61+
if (false === $attribute) {
62+
throw new RuntimeException('Attribute not found');
63+
}
64+
65+
unset($attribute['id']);
66+
67+
$count = (int)$input->getOption('count');
68+
69+
$query = $this->connection->createQueryBuilder();
70+
$query
71+
->insert('ezcontentobject_attribute');
72+
73+
foreach ($attribute as $key => $value) {
74+
$query->setValue($key, ':' . $key);
75+
$query->setParameter(':' . $key, $value);
76+
}
77+
78+
for ($i = 0; $i < $count; ++$i) {
79+
$query->execute();
80+
}
81+
82+
return Command::SUCCESS;
83+
}
84+
}

0 commit comments

Comments
 (0)