-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathMigrateCommand.php
More file actions
129 lines (102 loc) · 3.97 KB
/
MigrateCommand.php
File metadata and controls
129 lines (102 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
declare(strict_types=1);
namespace Netgen\Bundle\EnhancedSelectionBundle\Command;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Result;
use Netgen\Bundle\EnhancedSelectionBundle\Core\FieldType\EnhancedSelection\Type as EnhancedSelectionType;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use function count;
use function unserialize;
final class MigrateCommand extends Command
{
private Connection $db;
private string $typeIdentifier;
private SymfonyStyle $io;
public function __construct(Connection $db, EnhancedSelectionType $type)
{
$this->db = $db;
$this->typeIdentifier = $type->getFieldTypeIdentifier();
// Call to parent controller is mandatory in commands registered as services
parent::__construct();
}
protected function configure(): void
{
$this->setDescription('Migrates sckenhancedselection field type to version which stores content object data to database table.');
}
protected function initialize(InputInterface $input, OutputInterface $output): void
{
$this->io = new SymfonyStyle($input, $output);
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$statement = $this->getFields();
$this->io->progressStart($statement->rowCount());
while ($row = $statement->fetchAssociative()) {
if ($row['data_text'] !== null) {
$fieldId = (int) $row['id'];
$version = (int) $row['version'];
$this->removeSelectionDataForField($fieldId, $version);
$identifiers = (array) unserialize($row['data_text']);
if (count($identifiers) > 0) {
$this->createSelections($fieldId, $version, $identifiers);
}
$this->resetFieldData($fieldId, $version);
}
$this->io->progressAdvance();
}
$this->io->progressFinish();
return 0;
}
private function getFields(): Result
{
$builder = $this->db->createQueryBuilder();
$builder->select('a.id', 'a.version', 'a.data_text')
->from('ezcontentobject_attribute', 'a')
->where(
$builder->expr()->eq('a.data_type_string', ':data_type_string')
)
->setParameter('data_type_string', $this->typeIdentifier);
return $builder->executeQuery();
}
private function resetFieldData(int $id, int $version): void
{
$builder = $this->db->createQueryBuilder();
$builder->update('ezcontentobject_attribute')
->set('data_text', 'null')
->where(
$builder->expr()->eq('id', ':id')
)->andWhere(
$builder->expr()->eq('version', ':version')
)
->setParameter('id', $id)
->setParameter('version', $version);
$builder->executeStatement();
}
private function removeSelectionDataForField(int $id, int $version): void
{
$builder = $this->db->createQueryBuilder();
$builder->delete($this->typeIdentifier)
->where(
$builder->expr()->eq('contentobject_attribute_id', ':id')
)->andWhere(
$builder->expr()->eq('contentobject_attribute_version', ':version')
)
->setParameter('id', $id)
->setParameter('version', $version);
$builder->executeStatement();
}
private function createSelections(int $id, int $version, array $identifiers): void
{
$data = [
'contentobject_attribute_id' => $id,
'contentobject_attribute_version' => $version,
];
foreach ($identifiers as $identifier) {
$data['identifier'] = $identifier;
$this->db->insert($this->typeIdentifier, $data);
}
}
}