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