1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Sylius\Migrations; |
15
|
|
|
|
16
|
|
|
use Doctrine\DBAL\Schema\Schema; |
17
|
|
|
use Doctrine\Migrations\AbstractMigration; |
18
|
|
|
use Sylius\Component\Attribute\AttributeType\SelectAttributeType; |
19
|
|
|
use Sylius\Component\Product\Model\ProductAttributeInterface; |
20
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
21
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
22
|
|
|
|
23
|
|
|
class Version20171003103916 extends AbstractMigration implements ContainerAwareInterface |
24
|
|
|
{ |
25
|
|
|
/** @var ContainerInterface */ |
26
|
|
|
private $container; |
27
|
|
|
|
28
|
|
|
public function setContainer(?ContainerInterface $container = null): void |
29
|
|
|
{ |
30
|
|
|
$this->container = $container; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function up(Schema $schema): void |
34
|
|
|
{ |
35
|
|
|
$this->abortIf($this->connection->getDatabasePlatform()->getName() != 'mysql', 'Migration can only be executed safely on \'mysql\'.'); |
36
|
|
|
|
37
|
|
|
$defaultLocale = $this->container->getParameter('locale'); |
38
|
|
|
$productAttributeRepository = $this->container->get('sylius.repository.product_attribute'); |
39
|
|
|
|
40
|
|
|
$productAttributes = $productAttributeRepository |
41
|
|
|
->createQueryBuilder('o') |
42
|
|
|
->select([ |
43
|
|
|
'o.id', |
44
|
|
|
'o.configuration', |
45
|
|
|
]) |
46
|
|
|
->where('o.type = :type') |
47
|
|
|
->setParameter('type', SelectAttributeType::TYPE) |
48
|
|
|
->getQuery() |
49
|
|
|
->getResult() |
50
|
|
|
; |
51
|
|
|
|
52
|
|
|
/** @var ProductAttributeInterface $productAttribute */ |
53
|
|
|
foreach ($productAttributes as $productAttribute) { |
54
|
|
|
$configuration = $productAttribute->getConfiguration(); |
55
|
|
|
$upgradedConfiguration = []; |
56
|
|
|
|
57
|
|
|
foreach ($configuration as $configurationKey => $value) { |
58
|
|
|
if ('choices' === $configurationKey) { |
59
|
|
|
foreach ($value as $key => $choice) { |
60
|
|
|
$upgradedConfiguration[$configurationKey][$key][$defaultLocale] = $choice; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
continue; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$upgradedConfiguration[$configurationKey] = $value; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$this->addSql('UPDATE sylius_product_attribute SET configuration = :configuration WHERE id = :id', [ |
70
|
|
|
'id' => $productAttribute->getId(), |
71
|
|
|
'configuration' => serialize($upgradedConfiguration), |
72
|
|
|
]); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function down(Schema $schema): void |
77
|
|
|
{ |
78
|
|
|
$this->abortIf($this->connection->getDatabasePlatform()->getName() != 'mysql', 'Migration can only be executed safely on \'mysql\'.'); |
79
|
|
|
|
80
|
|
|
$defaultLocale = $this->container->getParameter('locale'); |
81
|
|
|
$productAttributeRepository = $this->container->get('sylius.repository.product_attribute'); |
82
|
|
|
|
83
|
|
|
$productAttributes = $productAttributeRepository->findBy(['type' => SelectAttributeType::TYPE]); |
84
|
|
|
/** @var ProductAttributeInterface $productAttribute */ |
85
|
|
|
foreach ($productAttributes as $productAttribute) { |
86
|
|
|
$configuration = $productAttribute->getConfiguration(); |
87
|
|
|
$downgradedConfiguration = []; |
88
|
|
|
|
89
|
|
|
foreach ($configuration as $configurationKey => $value) { |
90
|
|
|
if ('choices' === $configurationKey) { |
91
|
|
|
foreach ($value as $key => $choice) { |
92
|
|
|
if (array_key_exists($defaultLocale, $choice)) { |
93
|
|
|
$downgradedConfiguration[$configurationKey][$key] = $choice[$defaultLocale]; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
continue; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$downgradedConfiguration[$configurationKey] = $value; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$this->addSql('UPDATE sylius_product_attribute SET configuration = :configuration WHERE id = :id', [ |
104
|
|
|
'id' => $productAttribute->getId(), |
105
|
|
|
'configuration' => serialize($downgradedConfiguration), |
106
|
|
|
]); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|