Skip to content

Commit 63e5b48

Browse files
author
Minh Le
committed
Change getDataFromEditmode to also check for existing key instead of just id.
1 parent 2cfa62e commit 63e5b48

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

Model/DataObject/ClassDefinition/Data/ObjectBridge.php

+22-11
Original file line numberDiff line numberDiff line change
@@ -388,20 +388,28 @@ public function getDataFromEditmode($data, $object = null, $params = [])
388388
$bridgeObjectId = $this->getBridgeIdBySourceAndOwner($object, $bridgeClass, $sourceId);
389389

390390
$sourceObject = $sourceClass::getById($sourceId);
391+
// If there is no id check for existing key instead. This is to prevent creating multiple versions of the same bridge object.
391392
/** @var Concrete $bridgeObject */
392393
if (!$bridgeObjectId) {
393-
$bridgeObject = new $bridgeClass;
394-
$parent = Model\DataObject\Service::createFolderByPath($this->bridgeFolder);
395-
if (!$parent instanceof AbstractObject) {
396-
throw new \InvalidArgumentException(
397-
sprintf('Parent not found at "%s" please check your object bridge configuration "Bridge folder"', $this->bridgeFolder),
398-
1574671788
399-
);
394+
$key = $this->bridgePrefix . $object->getId() . '_' . $sourceObject->getId();
395+
$existingBridgeObject = $bridgeClass::getByKey($key)->getObjects();
396+
397+
if (!empty($existingBridgeObject)) {
398+
$bridgeObject = $existingBridgeObject[0];
399+
} else {
400+
$bridgeObject = new $bridgeClass;
401+
$parent = Model\DataObject\Service::createFolderByPath($this->bridgeFolder);
402+
if (!$parent instanceof AbstractObject) {
403+
throw new \InvalidArgumentException(
404+
sprintf('Parent not found at "%s" please check your object bridge configuration "Bridge folder"', $this->bridgeFolder),
405+
1574671788
406+
);
407+
}
408+
$bridgeObject->setParent($parent);
409+
$bridgeObject->setKey($this->bridgePrefix . $object->getId() . '_' . $sourceObject->getId());
410+
// Make sure its unique else saving will throw an error
411+
$bridgeObject->setKey(Model\DataObject\Service::getUniqueKey($bridgeObject));
400412
}
401-
$bridgeObject->setParent($parent);
402-
$bridgeObject->setKey($this->bridgePrefix . $object->getId() . '_' . $sourceObject->getId());
403-
// Make sure its unique else saving will throw an error
404-
$bridgeObject->setKey(Model\DataObject\Service::getUniqueKey($bridgeObject));
405413
} else {
406414
$bridgeObject = $bridgeClass::getById($bridgeObjectId);
407415
}
@@ -660,6 +668,9 @@ public function checkValidity($data, $omitMandatoryCheck = false, $params = [])
660668

661669
/** @var Concrete $objectBridge */
662670
foreach ($data as $objectBridge) {
671+
if (!$objectBridge) {
672+
continue;
673+
}
663674
$bridgeClassFullName = $this->getBridgeFullClassName();
664675
if (!($objectBridge instanceof $bridgeClassFullName)) {
665676
throw new Element\ValidationException('Expected ' . $bridgeClassFullName, 1574671790);

0 commit comments

Comments
 (0)