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