Skip to content

Commit 438d303

Browse files
Merge pull request #76 from JakobTolkemit/file_content
Update File content migration
2 parents 767acb9 + d0c3d9a commit 438d303

File tree

5 files changed

+114
-9
lines changed

5 files changed

+114
-9
lines changed

src/Entity/FileContent.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ class FileContent implements ResourceInterface
1414
/** @var string */
1515
private $content;
1616

17-
public function __construct($content)
17+
public function __construct(string $content)
1818
{
19-
$this->content = (string) $content;
19+
$this->content = $content;
2020
}
2121

2222
public function getId(): int

src/Entity/OrderItemOption.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class OrderItemOption implements OrderItemOptionInterface
6060
/** @var float */
6161
private $percent = 0;
6262

63-
/** @var FileContent */
63+
/** @var ?FileContent */
6464
private $fileContent;
6565

6666
/** {@inheritdoc} */
@@ -152,7 +152,7 @@ public function setCustomerOptionValue($value): void
152152
if ($this->customerOptionType === CustomerOptionTypeEnum::FILE) {
153153
$this->optionValue = 'file-content';
154154
$this->customerOptionValue = null;
155-
$this->fileContent = new FileContent($value);
155+
$this->fileContent = new FileContent((string) $value);
156156

157157
return;
158158
}

src/Migrations/Version20210303184909.php

Lines changed: 78 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,108 @@
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
{
15-
public function getDescription() : string
20+
/** @var ?ContainerInterface */
21+
private $container;
22+
23+
public function setContainer(?ContainerInterface $container = null): void
24+
{
25+
$this->container = $container;
26+
}
27+
28+
public function getDescription(): string
1629
{
1730
return '';
1831
}
1932

20-
public function up(Schema $schema) : void
33+
public function up(Schema $schema): void
2134
{
2235
// this up() migration is auto-generated, please modify it to your needs
2336
$this->addSql('CREATE TABLE brille24_customer_option_file_content (id INT AUTO_INCREMENT NOT NULL, content LONGTEXT NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET UTF8 COLLATE `UTF8_unicode_ci` ENGINE = InnoDB');
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

29-
public function down(Schema $schema) : void
52+
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
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Brille24\SyliusCustomerOptionsPlugin\Migrations;
6+
7+
use Doctrine\DBAL\Schema\Schema;
8+
use Doctrine\Migrations\AbstractMigration;
9+
10+
/**
11+
* Auto-generated Migration: Please modify to your needs!
12+
*/
13+
final class Version20210317090200 extends AbstractMigration
14+
{
15+
public function getDescription(): string
16+
{
17+
return '';
18+
}
19+
20+
public function up(Schema $schema): void
21+
{
22+
// this up() migration is auto-generated, please modify it to your needs
23+
$this->addSql('ALTER TABLE brille24_customer_option_order_item_option CHANGE optionValue optionValue VARCHAR(255) DEFAULT NULL');
24+
}
25+
26+
public function down(Schema $schema): void
27+
{
28+
// this down() migration is auto-generated, please modify it to your needs
29+
$this->addSql('ALTER TABLE brille24_customer_option_order_item_option CHANGE optionValue optionValue LONGTEXT CHARACTER SET utf8 DEFAULT NULL COLLATE `utf8_unicode_ci`');
30+
}
31+
}

src/Resources/config/doctrine/OrderItemOption.orm.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
<field name="customerOptionValueCode" nullable="true"/>
2121
<field name="customerOptionValueName" nullable="true"/>
22-
<field name="optionValue" nullable="true" type="text"/>
22+
<field name="optionValue" nullable="true" type="string"/>
2323
<one-to-one field="fileContent" target-entity="Brille24\SyliusCustomerOptionsPlugin\Entity\FileContent">
2424
<cascade>
2525
<cascade-all />

0 commit comments

Comments
 (0)