Skip to content

Commit

Permalink
Fix duplicate relations on event listeners save (pimcore#17408)
Browse files Browse the repository at this point in the history
* Moved __rawRelationData from Concrete to parent Class

* Removed __rawRelationsData from Concrete class

* Moved __getRawRelationData method to AbstractObject

* Apply php-cs-fixer changes

* Remove classId from Concrete, add to AbstractObject

* Apply php-cs-fixer changes

* Simplify getClassId method

* Update models/DataObject/AbstractObject.php

Co-authored-by: Sebastian Blank <[email protected]>

* Update models/DataObject/AbstractObject.php

Co-authored-by: Sebastian Blank <[email protected]>

---------

Co-authored-by: cuca24 <[email protected]>
Co-authored-by: Sebastian Blank <[email protected]>
  • Loading branch information
3 people authored Oct 18, 2024
1 parent 4c3a006 commit d8da787
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 48 deletions.
46 changes: 46 additions & 0 deletions models/DataObject/AbstractObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Pimcore;
use Pimcore\Cache;
use Pimcore\Cache\RuntimeCache;
use Pimcore\Db;
use Pimcore\Event\DataObjectEvents;
use Pimcore\Event\Model\DataObjectEvent;
use Pimcore\Logger;
Expand Down Expand Up @@ -118,6 +119,19 @@ abstract class AbstractObject extends Model\Element\AbstractElement
*/
protected ?string $childrenSortOrder = null;

/**
* @internal
*
* @var string|null
*/
protected $classId = null;

/**
* @internal
*
*/
protected ?array $__rawRelationData = null;

protected function getBlockedVars(): array
{
$blockedVars = ['versions', 'class', 'scheduledTasks', 'omitMandatoryCheck'];
Expand Down Expand Up @@ -596,6 +610,9 @@ public function save(array $parameters = []): static
// add to queue that saves dependencies
$this->addToDependenciesQueue();

//Reset Relational data to force a reload
$this->__rawRelationData = null;

$postEvent = new DataObjectEvent($this, $parameters);
if ($isUpdate) {
if ($differentOldPath) {
Expand Down Expand Up @@ -969,6 +986,35 @@ public function getChildrenSortOrder(): string
return $this->childrenSortOrder ?? self::OBJECT_CHILDREN_SORT_ORDER_DEFAULT;
}

/**
* @return $this
*/
public function setClassId(string $classId): static
{
$this->classId = $classId;

return $this;
}

public function getClassId(): ?string
{
return $this->classId;
}

/**
* @internal
*
*/
public function __getRawRelationData(): array
{
if ($this->__rawRelationData === null) {
$db = Db::get();
$this->__rawRelationData = $db->fetchAllAssociative('SELECT * FROM object_relations_' . $this->getClassId() . ' WHERE src_id = ?', [$this->getId()]);
}

return $this->__rawRelationData;
}

/**
* load lazy loaded fields before cloning
*/
Expand Down
48 changes: 0 additions & 48 deletions models/DataObject/Concrete.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,6 @@ class Concrete extends DataObject implements LazyLoadedFieldsInterface
use Model\DataObject\Traits\LazyLoadedRelationTrait;
use Model\Element\Traits\ScheduledTasksTrait;

/**
* @internal
*
*/
protected ?array $__rawRelationData = null;

/**
* @internal
*
Expand All @@ -74,13 +68,6 @@ class Concrete extends DataObject implements LazyLoadedFieldsInterface
*/
protected ?ClassDefinition $class = null;

/**
* @internal
*
* @var string|null
*/
protected $classId = null;

/**
* @internal
*
Expand Down Expand Up @@ -401,25 +388,6 @@ public function getClass(): ClassDefinition
return $this->class;
}

public function getClassId(): ?string
{
if (isset($this->classId)) {
return (string)$this->classId;
}

return null;
}

/**
* @return $this
*/
public function setClassId(string $classId): static
{
$this->classId = $classId;

return $this;
}

public function getClassName(): ?string
{
return $this->className;
Expand Down Expand Up @@ -667,8 +635,6 @@ public function save(array $parameters = []): static

try {
parent::save($parameters);
//Reset Relational data to force a reload
$this->__rawRelationData = null;

if ($this instanceof DirtyIndicatorInterface) {
$this->resetDirtyMap();
Expand Down Expand Up @@ -843,18 +809,4 @@ public function retrieveRelationData(array $descriptor): array

return $filteredData;
}

/**
* @internal
*
*/
public function __getRawRelationData(): array
{
if ($this->__rawRelationData === null) {
$db = Db::get();
$this->__rawRelationData = $db->fetchAllAssociative('SELECT * FROM object_relations_' . $this->getClassId() . ' WHERE src_id = ?', [$this->getId()]);
}

return $this->__rawRelationData;
}
}

0 comments on commit d8da787

Please sign in to comment.