Skip to content

Commit

Permalink
[Core] allow multiple store-values
Browse files Browse the repository at this point in the history
  • Loading branch information
dpfaffenbauer committed Nov 5, 2024
1 parent 36f7df1 commit 2528a85
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 5 deletions.
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,46 @@
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
{
return $this->findForObject($product, 'storeValues');
}

public function findForProductAndStore(ProductInterface $product, StoreInterface $store): ?ProductStoreValuesInterface
{
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(): ?string
{
return $this->fieldName;
}

public function setFieldName(?string $fieldName): void

Check failure on line 78 in src/CoreShop/Component/Core/Model/ProductStoreValues.php

View workflow job for this annotation

GitHub Actions / ~11.1.0, PHP 8.2, Deps highest

Return type (void) of method CoreShop\Component\Core\Model\ProductStoreValues::setFieldName() should be compatible with return type (string) of method CoreShop\Component\Core\Model\ProductStoreValuesInterface::setFieldName()
{
$this->fieldName = $fieldName;
}

public function getPrice()
{
return $this->price;
Expand Down
10 changes: 10 additions & 0 deletions src/CoreShop/Component/Core/Model/ProductStoreValuesInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@

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

/**
* @return string
*/
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;
}

0 comments on commit 2528a85

Please sign in to comment.