Skip to content

Commit 12baf3d

Browse files
author
Jakob Tolkemit
committed
Updated file content migration
1 parent 767acb9 commit 12baf3d

File tree

1 file changed

+75
-1
lines changed

1 file changed

+75
-1
lines changed

src/Migrations/Version20210303184909.php

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,27 @@
44

55
namespace Brille24\SyliusCustomerOptionsPlugin\Migrations;
66

7+
use Brille24\SyliusCustomerOptionsPlugin\Enumerations\CustomerOptionTypeEnum;
78
use Doctrine\DBAL\Schema\Schema;
89
use Doctrine\Migrations\AbstractMigration;
10+
use Doctrine\ORM\EntityManagerInterface;
11+
use Doctrine\Persistence\ManagerRegistry;
12+
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
13+
use Symfony\Component\DependencyInjection\ContainerInterface;
914

1015
/**
1116
* Auto-generated Migration: Please modify to your needs!
1217
*/
13-
final class Version20210303184909 extends AbstractMigration
18+
final class Version20210303184909 extends AbstractMigration implements ContainerAwareInterface
1419
{
20+
/** @var ?ContainerInterface */
21+
private $container;
22+
23+
public function setContainer(?ContainerInterface $container = null)
24+
{
25+
$this->container = $container;
26+
}
27+
1528
public function getDescription() : string
1629
{
1730
return '';
@@ -24,14 +37,75 @@ public function up(Schema $schema) : void
2437
$this->addSql('ALTER TABLE brille24_customer_option_order_item_option ADD fileContent_id INT DEFAULT NULL');
2538
$this->addSql('ALTER TABLE brille24_customer_option_order_item_option ADD CONSTRAINT FK_8B833EE486EBE56 FOREIGN KEY (fileContent_id) REFERENCES brille24_customer_option_file_content (id)');
2639
$this->addSql('CREATE UNIQUE INDEX UNIQ_8B833EE486EBE56 ON brille24_customer_option_order_item_option (fileContent_id)');
40+
41+
$id = 1;
42+
foreach ($this->getOrderItemOptionsWithValues() as $orderItemOption) {
43+
$fileContent = $orderItemOption['optionValue'];
44+
45+
$this->addSql('INSERT INTO brille24_customer_option_file_content (id, content) VALUES(:id, :content)', ['id' => $id, 'content' => $fileContent]);
46+
$this->addSql('UPDATE brille24_customer_option_order_item_option SET fileContent_id = :file_content_id, optionValue = "file-content" WHERE id = :id', ['file_content_id' => $id, 'id' => $orderItemOption['id']]);
47+
48+
$id++;
49+
}
2750
}
2851

2952
public function down(Schema $schema) : void
3053
{
54+
foreach ($this->getOrderItemOptionsWithFileContent() as $orderItemOption) {
55+
$fileContent = $orderItemOption['content'];
56+
$id = $orderItemOption['id'];
57+
58+
$this->addSql('UPDATE brille24_customer_option_order_item_option SET optionValue = :content WHERE id = :id', ['content' => $fileContent, 'id' => $id]);
59+
}
60+
3161
// this down() migration is auto-generated, please modify it to your needs
3262
$this->addSql('ALTER TABLE brille24_customer_option_order_item_option DROP FOREIGN KEY FK_8B833EE486EBE56');
3363
$this->addSql('DROP TABLE brille24_customer_option_file_content');
3464
$this->addSql('DROP INDEX UNIQ_8B833EE486EBE56 ON brille24_customer_option_order_item_option');
3565
$this->addSql('ALTER TABLE brille24_customer_option_order_item_option DROP fileContent_id');
3666
}
67+
68+
private function getOrderItemOptionsWithValues(): array
69+
{
70+
$productAttributeClass = $this->container->getParameter('brille24.model.order_item_option.class');
71+
72+
$entityManager = $this->getEntityManager($productAttributeClass);
73+
74+
return $entityManager->createQueryBuilder()
75+
->select('o.id, o.optionValue')
76+
->from($productAttributeClass, 'o')
77+
->where('o.customerOptionType = :type')
78+
->setParameter('type', CustomerOptionTypeEnum::FILE)
79+
->getQuery()
80+
->getArrayResult()
81+
;
82+
}
83+
84+
private function getOrderItemOptionsWithFileContent(): array
85+
{
86+
$productAttributeClass = $this->container->getParameter('brille24.model.order_item_option.class');
87+
88+
$entityManager = $this->getEntityManager($productAttributeClass);
89+
90+
return $entityManager->createQueryBuilder()
91+
->select('o.id, f.content')
92+
->from($productAttributeClass, 'o')
93+
->join('o.fileContent', 'f')
94+
->where('o.customerOptionType = :type')
95+
->setParameter('type', CustomerOptionTypeEnum::FILE)
96+
->getQuery()
97+
->getArrayResult()
98+
;
99+
}
100+
101+
private function getEntityManager(string $class): EntityManagerInterface
102+
{
103+
/** @var ManagerRegistry $managerRegistry */
104+
$managerRegistry = $this->container->get('doctrine');
105+
106+
/** @var EntityManagerInterface $manager */
107+
$manager = $managerRegistry->getManagerForClass($class);
108+
109+
return $manager;
110+
}
37111
}

0 commit comments

Comments
 (0)