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