1515
1616use Doctrine \DBAL \Schema \Schema ;
1717use Doctrine \Migrations \AbstractMigration ;
18+ use Doctrine \ORM \EntityManagerInterface ;
19+ use Doctrine \Persistence \ManagerRegistry ;
1820use Sylius \Component \Attribute \AttributeType \SelectAttributeType ;
1921use Sylius \Component \Product \Model \ProductAttributeInterface ;
2022use Symfony \Component \DependencyInjection \ContainerAwareInterface ;
@@ -32,12 +34,11 @@ public function setContainer(?ContainerInterface $container = null): void
3234
3335 public function up (Schema $ schema ): void
3436 {
35- $ this ->abortIf ($ this ->connection ->getDatabasePlatform ()->getName () != 'mysql ' , 'Migration can only be executed safely on \'mysql \'. ' );
37+ $ this ->abortIf ($ this ->connection ->getDatabasePlatform ()->getName () !== 'mysql ' , 'Migration can only be executed safely on \'mysql \'. ' );
3638
3739 $ defaultLocale = $ this ->container ->getParameter ('locale ' );
38- $ productAttributeRepository = $ this ->container -> get ( ' sylius.repository.product_attribute ' );
40+ $ productAttributes = $ this ->getProductAttributes ( );
3941
40- $ productAttributes = $ productAttributeRepository ->findBy (['type ' => SelectAttributeType::TYPE ]);
4142 /** @var ProductAttributeInterface $productAttribute */
4243 foreach ($ productAttributes as $ productAttribute ) {
4344 $ configuration = $ productAttribute ->getConfiguration ();
@@ -64,12 +65,11 @@ public function up(Schema $schema): void
6465
6566 public function down (Schema $ schema ): void
6667 {
67- $ this ->abortIf ($ this ->connection ->getDatabasePlatform ()->getName () != 'mysql ' , 'Migration can only be executed safely on \'mysql \'. ' );
68+ $ this ->abortIf ($ this ->connection ->getDatabasePlatform ()->getName () !== 'mysql ' , 'Migration can only be executed safely on \'mysql \'. ' );
6869
6970 $ defaultLocale = $ this ->container ->getParameter ('locale ' );
70- $ productAttributeRepository = $ this ->container -> get ( ' sylius.repository.product_attribute ' );
71+ $ productAttributes = $ this ->getProductAttributes ( );
7172
72- $ productAttributes = $ productAttributeRepository ->findBy (['type ' => SelectAttributeType::TYPE ]);
7373 /** @var ProductAttributeInterface $productAttribute */
7474 foreach ($ productAttributes as $ productAttribute ) {
7575 $ configuration = $ productAttribute ->getConfiguration ();
@@ -95,4 +95,31 @@ public function down(Schema $schema): void
9595 ]);
9696 }
9797 }
98+
99+ private function getProductAttributes (): array
100+ {
101+ $ productAttributeClass = $ this ->container ->getParameter ('sylius.model.product_attribute.class ' );
102+
103+ $ entityManager = $ this ->getEntityManager ($ productAttributeClass );
104+
105+ return $ entityManager ->createQueryBuilder ()
106+ ->select ('o.id, o.configuration ' )
107+ ->from ($ productAttributeClass , 'o ' )
108+ ->andWhere ('o.type = :type ' )
109+ ->setParameter ('type ' , SelectAttributeType::TYPE )
110+ ->getQuery ()
111+ ->getResult ()
112+ ;
113+ }
114+
115+ private function getEntityManager (string $ class ): EntityManagerInterface
116+ {
117+ /** @var ManagerRegistry $managerRegistry */
118+ $ managerRegistry = $ this ->container ->get ('doctrine ' );
119+
120+ /** @var EntityManagerInterface $manager */
121+ $ manager = $ managerRegistry ->getManagerForClass ($ class );
122+
123+ return $ manager ;
124+ }
98125}
0 commit comments