Skip to content

Commit

Permalink
Merge branch '3.2' into 4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dpfaffenbauer committed May 9, 2024
2 parents 354c09e + e3842ed commit b8e4349
Show file tree
Hide file tree
Showing 14 changed files with 101 additions and 29 deletions.
2 changes: 1 addition & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ x-php: &php
# step debugging with `trigger`.
# To configure PhpStorm for step-debugging with xdebug see: https://www.jetbrains.com/help/phpstorm/2021.3/zero-configuration-debugging.html#start-debugging-session
# See: https://xdebug.org/docs/all_settings#mode
XDEBUG_MODE: debug,develop
XDEBUG_MODE: debug
# This forces xdebug to always connect to the debug client running on docker host (host.docker.internal).
# It will work without further configuration with [Docker Desktop](https://www.docker.com/products/docker-desktop).
# See: https://xdebug.org/docs/all_settings#client_host
Expand Down
4 changes: 2 additions & 2 deletions src/CoreShop/Bundle/CoreBundle/CoreExtension/StoreValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -473,15 +473,15 @@ public function save(Localizedfield|\Pimcore\Model\DataObject\Fieldcollection\Da
foreach ($availableStoreValues as $availableStoreValuesEntity) {
if (!in_array($availableStoreValuesEntity->getId(), $validStoreValues, true)) {
$this->getEntityManager()->remove($availableStoreValuesEntity);
$this->getEntityManager()->flush($availableStoreValuesEntity);
}
}

foreach ($allStoreValues as $storeEntity) {
$this->getEntityManager()->persist($storeEntity);
$this->getEntityManager()->flush($storeEntity);
}

$this->getEntityManager()->flush();

//We have to set that here, values could change during persist due to copy or variant inheritance break
$object->setObjectVar($this->getName(), $allStoreValues);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<join-column name="currencyId" referenced-column-name="id" nullable="true"/>
<cascade>
<cascade-persist/>
<cascade-merge/>
</cascade>
</many-to-one>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@

<many-to-one field="store" target-entity="CoreShop\Component\Store\Model\StoreInterface">
<join-column name="store" referenced-column-name="id" on-delete="CASCADE"/>
<cascade>
<cascade-merge/>
</cascade>
</many-to-one>

<one-to-many field="productUnitDefinitionPrices" target-entity="CoreShop\Component\Core\Model\ProductUnitDefinitionPriceInterface" mapped-by="productStoreValues" orphan-removal="true">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@

<many-to-one field="productStoreValues" target-entity="CoreShop\Component\Core\Model\ProductStoreValuesInterface" inversed-by="productUnitDefinitionPrices">
<join-column name="product_store_values" referenced-column-name="id" on-delete="CASCADE"/>
<cascade>
<cascade-merge/>
</cascade>
</many-to-one>

</mapped-superclass>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
</one-to-many>

<many-to-many field="countries" target-entity="CoreShop\Component\Address\Model\CountryInterface" inversed-by="stores">
<cascade>
<cascade-merge/>
</cascade>
<join-table name="coreshop_store_countries">
<join-columns>
<join-column name="store_id" referenced-column-name="id" nullable="false"/>
Expand Down
5 changes: 5 additions & 0 deletions src/CoreShop/Bundle/IndexBundle/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public function install(): void
parent::install();
}

public function markAllMigrationsInstalled(): void
{
$this->markInstalled();
}

public function needsReloadAfterInstall(): bool
{
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ class Version20200224161101 extends AbstractMigration
{
public function up(Schema $schema): void
{
if ($schema->getTable('coreshop_index')->hasColumn('indexLastVersion')) {
return;
}

$this->addSql(' ALTER TABLE coreshop_index ADD indexLastVersion TINYINT(1) DEFAULT \'0\' NOT NULL;');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ public function save(Concrete|Model\DataObject\Objectbrick\Data\AbstractData|Mod
$productUnitDefinitions->setProduct($object);

$this->getEntityManager()->persist($productUnitDefinitions);
$this->getEntityManager()->flush($productUnitDefinitions);
$this->getEntityManager()->flush();
}
}

Expand Down Expand Up @@ -457,9 +457,12 @@ public function getVersionPreview(mixed $data, Concrete $object = null, array $p
$defaultUnit = $data->getDefaultUnitDefinition() instanceof ProductUnitDefinitionInterface && $data->getDefaultUnitDefinition()->getUnit() instanceof ProductUnitInterface ? $data->getDefaultUnitDefinition()->getUnit()->getName() : '--';

return sprintf(
'Default Unit: %s, additional units: %d',
'Default Unit: %s, additional units: %d (%s)',
$defaultUnit,
$data->getAdditionalUnitDefinitions()->count(),
implode(', ', array_map(static function(ProductUnitDefinitionInterface $unitDefinition) {
return sprintf('%s: %s %s', $unitDefinition->getId(), $unitDefinition->getConversionRate(), $unitDefinition->getUnitName());
}, $data->getUnitDefinitions()->toArray()))
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
</inverse-join-columns>
</join-table>
<cascade>
<cascade-all/>
<cascade-persist/>
</cascade>
<order-by>
<order-by-field name="sort" direction="ASC"/>
Expand All @@ -49,7 +49,7 @@
</inverse-join-columns>
</join-table>
<cascade>
<cascade-all/>
<cascade-persist/>
</cascade>
<order-by>
<order-by-field name="sort" direction="ASC"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,10 @@

<many-to-one field="unit" target-entity="CoreShop\Component\Product\Model\ProductUnitInterface" fetch="EAGER">
<join-column name="unit" referenced-column-name="id"/>
<cascade>
<cascade-merge/>
</cascade>
</many-to-one>

<many-to-one field="productUnitDefinitions" target-entity="CoreShop\Component\Product\Model\ProductUnitDefinitions" inversed-by="unitDefinitions">
<join-column name="product_unit_definitions" referenced-column-name="id" on-delete="CASCADE"/>
<cascade>
<cascade-merge/>
<cascade-persist/>
</cascade>
</many-to-one>
</mapped-superclass>
</doctrine-mapping>
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
<many-to-one field="unitDefinition" target-entity="CoreShop\Component\Product\Model\ProductUnitDefinitionInterface">
<join-column name="unit_definition" referenced-column-name="id" on-delete="CASCADE"/>
<cascade>
<cascade-merge/>
<cascade-persist/>
</cascade>
</many-to-one>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@
target-entity="CoreShop\Component\ProductQuantityPriceRules\Model\QuantityRangeInterface"
mapped-by="rule" orphan-removal="true" fetch="LAZY">
<cascade>
<cascade-persist/>
<cascade-remove/>
<cascade-all/>
</cascade>
<order-by>
<order-by-field name="rangeStartingFrom" direction="ASC" />
Expand Down
83 changes: 77 additions & 6 deletions src/CoreShop/Bundle/ResourceBundle/Doctrine/ORM/EntityMerger.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,8 @@ private function doMerge($entity, array &$visited): void
}

$visited[$oid] = $entity; // mark visited

if ($entity instanceof Proxy && !$entity->__isInitialized()) {
$entity->__load();
}

$class = $this->em->getClassMetadata($entity::class);
$entity = $this->loadEntity($entity);

if ($this->em->getUnitOfWork()->getEntityState($entity, UnitOfWork::STATE_DETACHED) !== UnitOfWork::STATE_MANAGED) {
$id = $class->getIdentifierValues($entity);
Expand All @@ -92,6 +88,8 @@ private function doMerge($entity, array &$visited): void
$this->cascadeMerge($entity, $visited);
$this->em->getUnitOfWork()->persist($entity);
} else {
$managedCopy = $this->loadEntity($managedCopy);

$this->checkAssociations($entity, $managedCopy, $visited);
$this->em->getUnitOfWork()->removeFromIdentityMap($managedCopy);
$this->em->getUnitOfWork()->registerManaged($entity, $id, $this->getData($managedCopy));
Expand All @@ -102,6 +100,24 @@ private function doMerge($entity, array &$visited): void
$this->cascadeMerge($entity, $visited);
}

protected function loadEntity($entity)
{
$class = $this->em->getClassMetadata($entity::class);

if ($entity instanceof Proxy && !$entity->__isInitialized()) {
$id = $class->getIdentifierValues($entity);
$uwEntity = $this->em->getUnitOfWork()->tryGetById($id, $class->getName());

if ($uwEntity) {
$this->em->getUnitOfWork()->removeFromIdentityMap($uwEntity);
}

$entity->__load();
}

return $entity;
}

/**
* @param object $entity
* @param object $managedCopy
Expand Down Expand Up @@ -278,7 +294,62 @@ private function cascadeMerge($entity, array &$visited): void
{
$class = $this->em->getClassMetadata($entity::class);

foreach ($class->associationMappings as $assoc) {
$associationMappings = array_filter(
$class->associationMappings,
static function ($assoc) {
return $assoc['isCascadeMerge'];
}
);
$noMergeAssociationMappings = array_filter(
$class->associationMappings,
static function ($assoc) {
return !$assoc['isCascadeMerge'];
}
);

/**
* Restore managed entities to their original state
*/
foreach ($noMergeAssociationMappings as $assoc) {
$relatedEntities = $class->reflFields[$assoc['fieldName']]->getValue($entity);

if (!$relatedEntities) {
continue;
}

if ($relatedEntities instanceof Collection) {
//Reset Collection
$pColl = new PersistentCollection($this->em, $assoc['targetEntity'], new ArrayCollection());
$pColl->setOwner($entity, $assoc);
$pColl->setInitialized(false);

$class->reflFields[$assoc['fieldName']]->setValue($entity, $pColl);
} else {
//Reset "tmp" entity with managed entity
$relatedEntityClass = $this->em->getClassMetadata($assoc['targetEntity']);
$id = $relatedEntityClass->getIdentifierValues($relatedEntities);

if (!$id) {
continue;
}

$uwEntity = $this->em->getUnitOfWork()->tryGetById($id, $relatedEntityClass->getName());

//Entity might not be loaded and managed yet, try to load it
if (!$uwEntity) {
$uwEntity = $this->em->find($relatedEntityClass->getName(), $id);
}

if ($uwEntity) {
$class->reflFields[$assoc['fieldName']]->setValue($entity, $uwEntity);
}
}
}

/**
* Merge related entities to the new state
*/
foreach ($associationMappings as $assoc) {
$relatedEntities = $class->reflFields[$assoc['fieldName']]->getValue($entity);

if ($relatedEntities instanceof Collection) {
Expand Down

0 comments on commit b8e4349

Please sign in to comment.