Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Core] allow multiple store-values #2747

Open
wants to merge 3 commits into
base: 4.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/CoreShop/Bundle/CoreBundle/CoreExtension/StoreValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
;
;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace CoreShop\Bundle\CoreBundle\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20241105123053 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
if ($schema->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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
<mapped-superclass name="CoreShop\Component\Core\Model\ProductStoreValues"
table="coreshop_product_store_values">
<unique-constraints>
<unique-constraint name="product_store" columns="product,store"/>
<unique-constraint name="product_store" columns="product,store,fieldName"/>
</unique-constraints>

<id name="id" column="id" type="integer">
<generator strategy="AUTO"/>
</id>

<field name="fieldName" column="fieldName" type="string"/>
<field name="product" column="product" type="pimcoreObject"/>
<field name="price" column="price" type="bigintInteger"/>

Expand Down
15 changes: 15 additions & 0 deletions src/CoreShop/Component/Core/Model/ProductStoreValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ class ProductStoreValues extends AbstractResource implements ProductStoreValuesI
*/
protected $id;

/**
* @var string|null
*/
protected $fieldName;

/**
* @var int
*/
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@

interface ProductStoreValuesInterface extends ResourceInterface, StoreAwareInterface
{
/**
* @return string
*/
public function getFieldName();

public function setFieldName(string $fieldName);

/**
* @return int
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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;
}