Skip to content

Commit

Permalink
[BUG, EC] Followup Data Hub Data Importer does not respect overwrite …
Browse files Browse the repository at this point in the history
…flags for Key mappings (#426)

* Update Direct.php

* Apply php-cs-fixer changes

* target only fieldName key

* Apply php-cs-fixer changes

* Added parameter to uniqueid as key assignment

* Apply php-cs-fixer changes

* Avoid bc break

* Apply php-cs-fixer changes

---------

Co-authored-by: kingjia90 <kingjia90@users.noreply.github.com>
Co-authored-by: Marco Perberschlager <marco.perberschlager@pimcore.com>
Co-authored-by: mcop1 <mcop1@users.noreply.github.com>
  • Loading branch information
4 people authored Nov 22, 2024
1 parent 3edd4cd commit 5a08c98
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
14 changes: 11 additions & 3 deletions src/Mapping/DataTarget/Direct.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,20 @@ protected function checkAssignData($newData, $valueContainer, $getter)
$fieldName = $fieldNameParts[2];
}

$fieldDefinition = $this->getFieldDefinition($valueContainer, $fieldName);
if ($this->writeIfTargetIsNotEmpty === false && !$fieldDefinition->isEmpty($currentData)) {
if ($this->fieldName === 'key') {
$currentDataIsEmpty = empty($currentData);
$newDataIsEmpty = empty($newData);
} else {
$fieldDefinition = $this->getFieldDefinition($valueContainer, $fieldName);
$currentDataIsEmpty = $fieldDefinition->isEmpty($currentData);
$newDataIsEmpty = $fieldDefinition->isEmpty($newData);
}

if ($this->writeIfTargetIsNotEmpty === false && !$currentDataIsEmpty) {
return false;
}

if ($this->writeIfSourceIsEmpty === false && $fieldDefinition->isEmpty($newData)) {
if ($this->writeIfSourceIsEmpty === false && $newDataIsEmpty) {
return false;
}

Expand Down
8 changes: 8 additions & 0 deletions src/Processing/ImportProcessingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ protected function processElement(string $configName, array $importDataRow, Reso
$event = new PreSaveEvent($configName, $importDataRow, $element);
$this->eventDispatcher->dispatch($event);

$this->checkKey($element);
$element->save();

$event = new PostSaveEvent($configName, $importDataRow, $element);
Expand Down Expand Up @@ -390,4 +391,11 @@ public function cancelImportAndCleanupQueue(string $configName): void
TmpStore::delete($infoEntryId);
$this->queueService->cleanupQueueItems($configName);
}

private function checkKey(ElementInterface $element): void
{
if (empty($element->getKey())) {
$element->setKey(uniqid('import-', true));
}
}
}
17 changes: 12 additions & 5 deletions src/Resolver/Factory/DataObjectFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

namespace Pimcore\Bundle\DataImporterBundle\Resolver\Factory;

use Exception;
use Pimcore\Bundle\DataImporterBundle\Exception\InvalidConfigurationException;
use Pimcore\Model\DataObject\ClassDefinition;
use Pimcore\Model\Element\ElementInterface;
Expand Down Expand Up @@ -42,6 +43,10 @@ public function setSubType(string $subType): void
$this->subType = $subType;
}

/**
* @throws InvalidConfigurationException
* @throws Exception
*/
public function createNewElement(): ElementInterface
{
$class = ClassDefinition::getById($this->subType);
Expand All @@ -52,12 +57,14 @@ public function createNewElement(): ElementInterface
$className = '\\Pimcore\\Model\\DataObject\\' . ucfirst($class->getName());
$element = $this->modelFactory->build($className);

if ($element instanceof ElementInterface) {
$element->setKey(uniqid('import-', true));

return $element;
if (!($element instanceof ElementInterface)) {
throw new InvalidConfigurationException(
"Object of class `{$this->subType}` could not be created."
);
}

throw new InvalidConfigurationException("Object of class `{$this->subType}` could not be created.");
$element->setKey(uniqid('import-', true));

return $element;
}
}
9 changes: 9 additions & 0 deletions src/Resolver/Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,15 @@ public function loadOrCreateAndPrepareElement(array $inputData, bool $createNew

if (empty($element)) {
$element = $this->getElementFactory()->createNewElement();
/**
* Reset key for new element, otherwise a key, that gets passed in the input data, would not be used.
* See checkKey method in ImportProcessingService, which adds the key again if necessary.
*
* @see \Pimcore\Bundle\DataImporterBundle\Processing\ImportProcessingService::checkKey
*
* Needs to be done here, because adding a parameter to createNewElement would be a bc break.
*/
$element->setKey('');
$this->getCreateLocationStrategy()->updateParent($element, $inputData);
$justCreated = true;
} else {
Expand Down

0 comments on commit 5a08c98

Please sign in to comment.