-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DO NOT MERGE] Duplicate fields command
- Loading branch information
1 parent
30197d1
commit 544fccd
Showing
2 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\Bundle\Core\Command; | ||
|
||
use Doctrine\DBAL\Connection; | ||
use RuntimeException; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
final class DuplicateFieldsCommand extends Command | ||
{ | ||
/** @var \Doctrine\DBAL\Connection */ | ||
private $connection; | ||
|
||
public function __construct( | ||
Connection $connection | ||
) { | ||
parent::__construct('ibexa:content:duplicate-fields'); | ||
|
||
$this->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; | ||
} | ||
} |