diff --git a/src/CoreShop/Bundle/CoreBundle/CoreExtension/StoreValues.php b/src/CoreShop/Bundle/CoreBundle/CoreExtension/StoreValues.php index 206752faab..a62b6ffae3 100644 --- a/src/CoreShop/Bundle/CoreBundle/CoreExtension/StoreValues.php +++ b/src/CoreShop/Bundle/CoreBundle/CoreExtension/StoreValues.php @@ -401,7 +401,7 @@ public function createDataCopy(Concrete $object, mixed $data): mixed public function load(Localizedfield|\Pimcore\Model\DataObject\Fieldcollection\Data\AbstractData|AbstractData|Concrete $object, array $params = []): mixed { if (isset($params['force']) && $params['force']) { - return $this->getProductStoreValuesRepository()->findForProduct($object); + return $this->getProductStoreValuesRepository()->findForObject($object, $this->getName()); } return null; @@ -451,14 +451,17 @@ public function save(Localizedfield|\Pimcore\Model\DataObject\Fieldcollection\Da if (count($changeSet) > 0) { $productStoreValue = clone $productStoreValue; $productStoreValue->setProduct($object); + $productStoreValue->setFieldName($this->getName()); } } else { $productStoreValue->setProduct($object); + $productStoreValue->setFieldName($this->getName()); } } if (null === $productStoreValue->getProduct()) { $productStoreValue->setProduct($object); + $productStoreValue->setFieldName($this->getName()); } $this->getEntityManager()->persist($productStoreValue); @@ -669,6 +672,7 @@ public function getDataFromEditmode($data, $object = null, $params = []): mixed if ($storeValuesEntity instanceof ProductStoreValuesInterface && $storeValuesEntity->getProduct() && $storeValuesEntity->getProduct()->getId() !== $object->getId()) { $storeValuesEntity = clone $storeValuesEntity; $storeValuesEntity->setProduct($object); + $storeValuesEntity->setFieldName($this->getName()); } $form = $this->getFormFactory()->createNamed('', ProductStoreValuesType::class, $storeValuesEntity); @@ -735,6 +739,7 @@ public function createNew($object, StoreInterface $store) $newObject = $this->getFactory()->createNew(); $newObject->setStore($store); $newObject->setProduct($object); + $newObject->setFieldName($this->getName()); return $newObject; } diff --git a/src/CoreShop/Bundle/CoreBundle/Doctrine/ORM/ProductStoreValuesRepository.php b/src/CoreShop/Bundle/CoreBundle/Doctrine/ORM/ProductStoreValuesRepository.php index 7d88ec9a0e..7595b61e66 100644 --- a/src/CoreShop/Bundle/CoreBundle/Doctrine/ORM/ProductStoreValuesRepository.php +++ b/src/CoreShop/Bundle/CoreBundle/Doctrine/ORM/ProductStoreValuesRepository.php @@ -23,28 +23,54 @@ use CoreShop\Component\Core\Model\ProductStoreValuesInterface; use CoreShop\Component\Core\Repository\ProductStoreValuesRepositoryInterface; use CoreShop\Component\Store\Model\StoreInterface; +use Pimcore\Model\DataObject\Concrete; class ProductStoreValuesRepository extends EntityRepository implements ProductStoreValuesRepositoryInterface { public function findForProduct(ProductInterface $product): array + { + if (!$product instanceof Concrete) { + throw new \InvalidArgumentException('Product must be instance of ' . Concrete::class); + } + + return $this->findForObject($product, 'storeValues'); + } + + public function findForProductAndStore(ProductInterface $product, StoreInterface $store): ?ProductStoreValuesInterface + { + if (!$product instanceof Concrete) { + throw new \InvalidArgumentException('Product must be instance of ' . Concrete::class); + } + + return $this->findForObjectAndStore($product, 'storeValues', $store); + } + + public function findForObject(Concrete $product, string $fieldName): array { return $this->createQueryBuilder('o') ->andWhere('o.product = :product') + ->andWhere('o.fieldName = :fieldName') ->setParameter('product', $product->getId()) + ->setParameter('fieldName', $fieldName) ->getQuery() ->getResult() ; } - public function findForProductAndStore(ProductInterface $product, StoreInterface $store): ?ProductStoreValuesInterface - { + public function findForObjectAndStore( + Concrete $product, + string $fieldName, + StoreInterface $store + ): ?ProductStoreValuesInterface { return $this->createQueryBuilder('o') ->andWhere('o.product = :product') + ->andWhere('o.fieldName = :fieldName') ->andWhere('o.store = :store') ->setParameter('product', $product->getId()) ->setParameter('store', $store) + ->setParameter('fieldName', $fieldName) ->getQuery() ->getOneOrNullResult() - ; + ; } } diff --git a/src/CoreShop/Bundle/CoreBundle/Migrations/Version20241105123053.php b/src/CoreShop/Bundle/CoreBundle/Migrations/Version20241105123053.php new file mode 100644 index 0000000000..9ec36e20a7 --- /dev/null +++ b/src/CoreShop/Bundle/CoreBundle/Migrations/Version20241105123053.php @@ -0,0 +1,36 @@ +getTable('coreshop_product_store_values')->hasColumn('fieldName')) { + return; + } + + $this->addSql("ALTER TABLE coreshop_product_store_values ADD fieldName VARCHAR(255) NOT NULL;"); + $this->addSql("DROP INDEX product_store ON coreshop_product_store_values;"); + $this->addSql("CREATE UNIQUE INDEX product_store ON coreshop_product_store_values (product, store, fieldName);"); + $this->addSql("UPDATE coreshop_product_store_values SET fieldName='storeValues';"); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + } +} diff --git a/src/CoreShop/Bundle/CoreBundle/Resources/config/doctrine/model/ProductStoreValues.orm.xml b/src/CoreShop/Bundle/CoreBundle/Resources/config/doctrine/model/ProductStoreValues.orm.xml index 50cfba6193..6ee672dc09 100644 --- a/src/CoreShop/Bundle/CoreBundle/Resources/config/doctrine/model/ProductStoreValues.orm.xml +++ b/src/CoreShop/Bundle/CoreBundle/Resources/config/doctrine/model/ProductStoreValues.orm.xml @@ -4,13 +4,14 @@ - + + diff --git a/src/CoreShop/Component/Core/Model/ProductStoreValues.php b/src/CoreShop/Component/Core/Model/ProductStoreValues.php index 28cece7444..b48a96b823 100644 --- a/src/CoreShop/Component/Core/Model/ProductStoreValues.php +++ b/src/CoreShop/Component/Core/Model/ProductStoreValues.php @@ -35,6 +35,11 @@ class ProductStoreValues extends AbstractResource implements ProductStoreValuesI */ protected $id; + /** + * @var string|null + */ + protected $fieldName; + /** * @var int */ @@ -65,6 +70,16 @@ public function setId(int $id) $this->id = $id; } + public function getFieldName() + { + return $this->fieldName; + } + + public function setFieldName(?string $fieldName) + { + $this->fieldName = $fieldName; + } + public function getPrice() { return $this->price; diff --git a/src/CoreShop/Component/Core/Model/ProductStoreValuesInterface.php b/src/CoreShop/Component/Core/Model/ProductStoreValuesInterface.php index 9b41f5e359..8baf9cbf77 100644 --- a/src/CoreShop/Component/Core/Model/ProductStoreValuesInterface.php +++ b/src/CoreShop/Component/Core/Model/ProductStoreValuesInterface.php @@ -24,6 +24,13 @@ interface ProductStoreValuesInterface extends ResourceInterface, StoreAwareInterface { + /** + * @return string + */ + public function getFieldName(); + + public function setFieldName(string $fieldName); + /** * @return int */ diff --git a/src/CoreShop/Component/Core/Repository/ProductStoreValuesRepositoryInterface.php b/src/CoreShop/Component/Core/Repository/ProductStoreValuesRepositoryInterface.php index 8e6965cf38..00d4626687 100644 --- a/src/CoreShop/Component/Core/Repository/ProductStoreValuesRepositoryInterface.php +++ b/src/CoreShop/Component/Core/Repository/ProductStoreValuesRepositoryInterface.php @@ -22,6 +22,7 @@ use CoreShop\Component\Core\Model\ProductStoreValuesInterface; use CoreShop\Component\Resource\Repository\RepositoryInterface; use CoreShop\Component\Store\Model\StoreInterface; +use Pimcore\Model\DataObject\Concrete; interface ProductStoreValuesRepositoryInterface extends RepositoryInterface { @@ -31,4 +32,11 @@ interface ProductStoreValuesRepositoryInterface extends RepositoryInterface public function findForProduct(ProductInterface $product): array; public function findForProductAndStore(ProductInterface $product, StoreInterface $store): ?ProductStoreValuesInterface; + + /** + * @return ProductStoreValuesInterface[] + */ + public function findForObject(Concrete $product, string $fieldName): array; + + public function findForObjectAndStore(Concrete $product, string $fieldName, StoreInterface $store): ?ProductStoreValuesInterface; }