From 1688ad8bb95a43678360d114b540c030eb4e7bc2 Mon Sep 17 00:00:00 2001 From: Dominik Pfaffenbauer Date: Tue, 10 Jul 2018 16:31:32 +0100 Subject: [PATCH 1/7] [CoreBundle, ProductBundle] make Product Specific Price Rules extension and Store Price Extension lazy loading for better performance --- .../CoreBundle/CoreExtension/StorePrice.php | 18 +++++++++++++++++- .../ProductSpecificPriceRules.php | 17 +++++++++++------ 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/CoreShop/Bundle/CoreBundle/CoreExtension/StorePrice.php b/src/CoreShop/Bundle/CoreBundle/CoreExtension/StorePrice.php index c834cfa37d..e2bbe969b9 100644 --- a/src/CoreShop/Bundle/CoreBundle/CoreExtension/StorePrice.php +++ b/src/CoreShop/Bundle/CoreBundle/CoreExtension/StorePrice.php @@ -174,6 +174,7 @@ public function getGetterCode($class) $code .= '* @return ' . $this->getPhpdocType() . "\n"; $code .= '*/' . "\n"; $code .= 'public function get' . ucfirst($key) . ' (\CoreShop\Component\Store\Model\StoreInterface $store = null) {' . "\n"; + $code .= "\t".'$this->'.$key.' = $this->getClass()->getFieldDefinition("'.$key.'")->preGetData($this);'."\n"; $code .= "\t" . 'if (is_null($store)) {' . "\n"; $code .= "\t\t" . 'return $this->' . $key . ";\n"; $code .= "\t" . '}' . "\n"; @@ -214,11 +215,26 @@ public function getSetterCode($class) return $code; } + /** + * {@inheritdoc} + */ + public function getDataFromResource($data, $object = null, $params = []) + { + return []; + } + + /** + * {@inheritdoc} + */ + public function preGetData($object, $params = []) + { + return $this->loadData($object); + } /** * {@inheritdoc} */ - public function load($object) + public function loadData($object) { $prices = $this->getProductStorePriceRepository()->findForProductAndProperty($object, $this->getName()); $data = []; diff --git a/src/CoreShop/Bundle/ProductBundle/CoreExtension/ProductSpecificPriceRules.php b/src/CoreShop/Bundle/ProductBundle/CoreExtension/ProductSpecificPriceRules.php index c91659d57d..b9c77335d0 100644 --- a/src/CoreShop/Bundle/ProductBundle/CoreExtension/ProductSpecificPriceRules.php +++ b/src/CoreShop/Bundle/ProductBundle/CoreExtension/ProductSpecificPriceRules.php @@ -100,7 +100,15 @@ public function preGetData($object) { Assert::isInstanceOf($object, ProductInterface::class); - return $this->getProductSpecificPriceRuleRepository()->findForProduct($object); + return $this->loadData($object); + } + + /** + * {@inheritdoc} + */ + public function getDataFromResource($data, $object = null, $params = []) + { + return []; } /** @@ -210,12 +218,9 @@ public function save($object, $params = []) } /** - * @param $object - * @param array $params - * - * @return ProductSpecificPriceRuleInterface[] + * {@inheritdoc} */ - public function load($object, $params = []) + public function loadData($object, $params = []) { return $this->getProductSpecificPriceRuleRepository()->findForProduct($object); } From b51f327d045229c01e05facf2ce1610c0c36bd0e Mon Sep 17 00:00:00 2001 From: Dominik Pfaffenbauer Date: Tue, 10 Jul 2018 16:34:58 +0100 Subject: [PATCH 2/7] [CoreBundle, ProductBundle] make Product Specific Price Rules extension and Store Price Extension lazy loading for better performance --- .../ProductBundle/CoreExtension/ProductSpecificPriceRules.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/CoreShop/Bundle/ProductBundle/CoreExtension/ProductSpecificPriceRules.php b/src/CoreShop/Bundle/ProductBundle/CoreExtension/ProductSpecificPriceRules.php index b9c77335d0..f34932f051 100644 --- a/src/CoreShop/Bundle/ProductBundle/CoreExtension/ProductSpecificPriceRules.php +++ b/src/CoreShop/Bundle/ProductBundle/CoreExtension/ProductSpecificPriceRules.php @@ -127,7 +127,7 @@ public function getDataForEditmode($data, $object = null, $params = []) ]; if ($object instanceof ProductInterface) { - $prices = $this->load($object, $params); + $prices = $this->loadData($object, $params); $context = SerializationContext::create(); $context->setSerializeNull(true); @@ -191,7 +191,7 @@ public function save($object, $params = []) $getter = $this->getName(); $existingPriceRules = $object->$getter; - $all = $this->load($object, $params); + $all = $this->loadData($object, $params); $founds = []; if (is_array($existingPriceRules)) { From 028602ea33bf89b1946b681c5665d818de9375df Mon Sep 17 00:00:00 2001 From: Dominik Pfaffenbauer Date: Tue, 10 Jul 2018 18:06:25 +0100 Subject: [PATCH 3/7] [CoreBundle, ProductBundle] proper lazy loading for StorePrice and ProductSpecificPriceRules --- .../CoreBundle/CoreExtension/StorePrice.php | 49 ++++++++++++++----- .../ProductSpecificPriceRules.php | 36 ++++++++++++-- 2 files changed, 69 insertions(+), 16 deletions(-) diff --git a/src/CoreShop/Bundle/CoreBundle/CoreExtension/StorePrice.php b/src/CoreShop/Bundle/CoreBundle/CoreExtension/StorePrice.php index e2bbe969b9..22f970fc3e 100644 --- a/src/CoreShop/Bundle/CoreBundle/CoreExtension/StorePrice.php +++ b/src/CoreShop/Bundle/CoreBundle/CoreExtension/StorePrice.php @@ -197,7 +197,7 @@ public function getSetterCode($class) $code .= '*/' . "\n"; $code .= 'public function set' . ucfirst($key) . ' ($' . $key . ', \CoreShop\Component\Store\Model\StoreInterface $store = null) {' . "\n"; $code .= "\t" . 'if (is_null($' . $key . ')) {' . "\n"; - $code .= "\t\t" . '$' . $key . ' = [];' . "\n"; + $code .= "\t\t" . 'return $this;' . "\n"; $code .= "\t" . '}' . "\n"; $code .= "\t" . "\n"; $code .= "\t" . 'if (!is_int($' . $key . ') && !is_array($' . $key . ')) {' . "\n"; @@ -209,6 +209,7 @@ public function getSetterCode($class) $code .= "\t" . 'else if (!is_null($store)) {' . "\n"; $code .= "\t\t" . '$this->' . $key . '[$store->getId()] = $' . $key . ';' . "\n"; $code .= "\t" . '}' . "\n"; + $code .= "\t" . '$this->' . $key . ' = ' . '$this->getClass()->getFieldDefinition("' . $key . '")->preSetData($this, $this->' . $key . ');' . "\n"; $code .= "\t" . 'return $this;' . "\n"; $code .= "}\n\n"; @@ -228,27 +229,53 @@ public function getDataFromResource($data, $object = null, $params = []) */ public function preGetData($object, $params = []) { - return $this->loadData($object); + $data = $object->{$this->getName()}; + if (!in_array($this->getName(), $object->getO__loadedLazyFields())) { + $data = $this->load($object, ['force' => true]); + + $setter = 'set' . ucfirst($this->getName()); + if (method_exists($object, $setter)) { + $object->$setter($data); + } + } + + return $data; } /** * {@inheritdoc} */ - public function loadData($object) + public function preSetData($object, $data, $params = []) { - $prices = $this->getProductStorePriceRepository()->findForProductAndProperty($object, $this->getName()); - $data = []; - - /** - * @var $price ProductStorePriceInterface - */ - foreach ($prices as $price) { - $data[$price->getStore()->getId()] = $price->getPrice(); + if (!in_array($this->getName(), $object->getO__loadedLazyFields())) { + $object->addO__loadedLazyField($this->getName()); } return $data; } + /** + * {@inheritdoc} + */ + public function load($object, $params = []) + { + if (isset($params['force']) && $params['force']) { + $prices = $this->getProductStorePriceRepository()->findForProductAndProperty($object, $this->getName()); + $data = []; + + /** + * @var $price ProductStorePriceInterface + */ + foreach ($prices as $price) { + $data[$price->getStore()->getId()] = $price->getPrice(); + } + + return $data; + } + + return null; + } + /** * {@inheritdoc} */ diff --git a/src/CoreShop/Bundle/ProductBundle/CoreExtension/ProductSpecificPriceRules.php b/src/CoreShop/Bundle/ProductBundle/CoreExtension/ProductSpecificPriceRules.php index f34932f051..b26293f888 100644 --- a/src/CoreShop/Bundle/ProductBundle/CoreExtension/ProductSpecificPriceRules.php +++ b/src/CoreShop/Bundle/ProductBundle/CoreExtension/ProductSpecificPriceRules.php @@ -100,7 +100,29 @@ public function preGetData($object) { Assert::isInstanceOf($object, ProductInterface::class); - return $this->loadData($object); + $data = $object->{$this->getName()}; + if (!in_array($this->getName(), $object->getO__loadedLazyFields())) { + $data = $this->load($object, ['force' => true]); + + $setter = 'set' . ucfirst($this->getName()); + if (method_exists($object, $setter)) { + $object->$setter($data); + } + } + + return $data; + } + + /** + * {@inheritdoc} + */ + public function preSetData($object, $data, $params = []) + { + if (!in_array($this->getName(), $object->getO__loadedLazyFields())) { + $object->addO__loadedLazyField($this->getName()); + } + + return $data; } /** @@ -127,7 +149,7 @@ public function getDataForEditmode($data, $object = null, $params = []) ]; if ($object instanceof ProductInterface) { - $prices = $this->loadData($object, $params); + $prices = $this->load($object, ['force' => true]); $context = SerializationContext::create(); $context->setSerializeNull(true); @@ -191,7 +213,7 @@ public function save($object, $params = []) $getter = $this->getName(); $existingPriceRules = $object->$getter; - $all = $this->loadData($object, $params); + $all = $this->load($object, ['force' => true]); $founds = []; if (is_array($existingPriceRules)) { @@ -220,9 +242,13 @@ public function save($object, $params = []) /** * {@inheritdoc} */ - public function loadData($object, $params = []) + public function load($object, $params = []) { - return $this->getProductSpecificPriceRuleRepository()->findForProduct($object); + if (isset($params['force']) && $params['force']) { + return $this->getProductSpecificPriceRuleRepository()->findForProduct($object); + } + + return null; } /** From 03f22555f2ab72036652a032ab603134368261e6 Mon Sep 17 00:00:00 2001 From: Dominik Pfaffenbauer Date: Tue, 10 Jul 2018 18:36:37 +0100 Subject: [PATCH 4/7] [CoreBundle, ProductBundle] fix php unit tests --- .../lib/CoreShop/Test/PHPUnit/Suites/Product/SpecificPrice.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/lib/CoreShop/Test/PHPUnit/Suites/Product/SpecificPrice.php b/tests/lib/CoreShop/Test/PHPUnit/Suites/Product/SpecificPrice.php index 21b52158f6..6afd9737e8 100644 --- a/tests/lib/CoreShop/Test/PHPUnit/Suites/Product/SpecificPrice.php +++ b/tests/lib/CoreShop/Test/PHPUnit/Suites/Product/SpecificPrice.php @@ -132,6 +132,8 @@ protected function createRule() $priceRule->setActive(true); $priceRule->setProduct($this->product->getId()); + $this->product->setSpecificPriceRules([$priceRule]); + return $priceRule; } From db6920f7bccedc4b97a65eefa449ba3ac2a9b396 Mon Sep 17 00:00:00 2001 From: Dominik Pfaffenbauer Date: Tue, 10 Jul 2018 18:54:31 +0100 Subject: [PATCH 5/7] [CoreBundle, ProductBundle] fix php unit tests --- .../Test/PHPUnit/Suites/Product/SpecificPrice.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/lib/CoreShop/Test/PHPUnit/Suites/Product/SpecificPrice.php b/tests/lib/CoreShop/Test/PHPUnit/Suites/Product/SpecificPrice.php index 6afd9737e8..8c3a1aaf76 100644 --- a/tests/lib/CoreShop/Test/PHPUnit/Suites/Product/SpecificPrice.php +++ b/tests/lib/CoreShop/Test/PHPUnit/Suites/Product/SpecificPrice.php @@ -309,6 +309,8 @@ public function testPriceRuleActionDiscountAmount() $this->getEntityManager()->remove($rule); $this->getEntityManager()->flush(); + + $this->product->setSpecificPriceRules([]); } /** @@ -337,6 +339,8 @@ public function testPriceRuleActionDiscountPercent() $this->getEntityManager()->remove($rule); $this->getEntityManager()->flush(); + + $this->product->setSpecificPriceRules([]); } /** @@ -366,6 +370,8 @@ public function testPriceRuleActionNewPrice() $this->getEntityManager()->remove($rule); $this->getEntityManager()->flush(); + + $this->product->setSpecificPriceRules([]); } /** @@ -397,5 +403,7 @@ public function testPriceRuleActionDiscountPrice() $this->getEntityManager()->remove($rule); $this->getEntityManager()->flush(); + + $this->product->setSpecificPriceRules([]); } } From 5bc69e7274c958aa6b29509a6e1e4e0de6d6bf8b Mon Sep 17 00:00:00 2001 From: Dominik Pfaffenbauer Date: Thu, 19 Jul 2018 17:16:33 +0200 Subject: [PATCH 6/7] [CoreBundle] rebuild classes migration --- .../Migrations/Version20180719151524.php | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/CoreShop/Bundle/CoreBundle/Migrations/Version20180719151524.php diff --git a/src/CoreShop/Bundle/CoreBundle/Migrations/Version20180719151524.php b/src/CoreShop/Bundle/CoreBundle/Migrations/Version20180719151524.php new file mode 100644 index 0000000000..677017d2a1 --- /dev/null +++ b/src/CoreShop/Bundle/CoreBundle/Migrations/Version20180719151524.php @@ -0,0 +1,37 @@ +load(); + + foreach ($classes as $class) { + $class->save(); + } + + } + + /** + * @param Schema $schema + */ + public function down(Schema $schema) + { + // this down() migration is auto-generated, please modify it to your needs + + } +} From 7d5df2f471491ba452173f11a17fd29576fda364 Mon Sep 17 00:00:00 2001 From: Dominik Pfaffenbauer Date: Thu, 19 Jul 2018 17:19:51 +0200 Subject: [PATCH 7/7] [CoreBundle] only re-save classes with StorePrice or ProductSpecificPriceRule --- .../CoreBundle/Migrations/Version20180719151524.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/CoreShop/Bundle/CoreBundle/Migrations/Version20180719151524.php b/src/CoreShop/Bundle/CoreBundle/Migrations/Version20180719151524.php index 677017d2a1..263f07a61d 100644 --- a/src/CoreShop/Bundle/CoreBundle/Migrations/Version20180719151524.php +++ b/src/CoreShop/Bundle/CoreBundle/Migrations/Version20180719151524.php @@ -2,6 +2,8 @@ namespace CoreShop\Bundle\CoreBundle\Migrations; +use CoreShop\Bundle\CoreBundle\CoreExtension\StorePrice; +use CoreShop\Bundle\ProductBundle\CoreExtension\ProductSpecificPriceRules; use Doctrine\DBAL\Schema\Schema; use Pimcore\Migrations\Migration\AbstractPimcoreMigration; use Pimcore\Model\DataObject\ClassDefinition; @@ -21,7 +23,12 @@ public function up(Schema $schema) $classes = $classes->load(); foreach ($classes as $class) { - $class->save(); + foreach ($class->getFieldDefinitions() as $definition) { + if ($definition instanceof StorePrice || $definition instanceof ProductSpecificPriceRules) { + $class->save(); + break; + } + } } }