|
4 | 4 |
|
5 | 5 | namespace Brille24\SyliusCustomerOptionsPlugin\Migrations; |
6 | 6 |
|
| 7 | +use Brille24\SyliusCustomerOptionsPlugin\Enumerations\CustomerOptionTypeEnum; |
7 | 8 | use Doctrine\DBAL\Schema\Schema; |
8 | 9 | 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; |
9 | 14 |
|
10 | 15 | /** |
11 | 16 | * Auto-generated Migration: Please modify to your needs! |
12 | 17 | */ |
13 | | -final class Version20210303184909 extends AbstractMigration |
| 18 | +final class Version20210303184909 extends AbstractMigration implements ContainerAwareInterface |
14 | 19 | { |
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 |
16 | 29 | { |
17 | 30 | return ''; |
18 | 31 | } |
19 | 32 |
|
20 | | - public function up(Schema $schema) : void |
| 33 | + public function up(Schema $schema): void |
21 | 34 | { |
22 | 35 | // this up() migration is auto-generated, please modify it to your needs |
23 | 36 | $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'); |
24 | 37 | $this->addSql('ALTER TABLE brille24_customer_option_order_item_option ADD fileContent_id INT DEFAULT NULL'); |
25 | 38 | $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)'); |
26 | 39 | $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 | + } |
27 | 50 | } |
28 | 51 |
|
29 | | - public function down(Schema $schema) : void |
| 52 | + public function down(Schema $schema): void |
30 | 53 | { |
| 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 | + |
31 | 61 | // this down() migration is auto-generated, please modify it to your needs |
32 | 62 | $this->addSql('ALTER TABLE brille24_customer_option_order_item_option DROP FOREIGN KEY FK_8B833EE486EBE56'); |
33 | 63 | $this->addSql('DROP TABLE brille24_customer_option_file_content'); |
34 | 64 | $this->addSql('DROP INDEX UNIQ_8B833EE486EBE56 ON brille24_customer_option_order_item_option'); |
35 | 65 | $this->addSql('ALTER TABLE brille24_customer_option_order_item_option DROP fileContent_id'); |
36 | 66 | } |
| 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 | + } |
37 | 111 | } |
0 commit comments