Skip to content

Commit

Permalink
[Properties] Update properties only with dirty state (#16010)
Browse files Browse the repository at this point in the history
* [Properties] Update properties only with dirty state - #9720

* Save dependencies irrespective of properties dirty check

* Mark dirty on setProperty

* remove unused & docs changes
  • Loading branch information
dvesh3 committed Sep 27, 2023
1 parent e86896a commit 2ad37be
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 74 deletions.
1 change: 1 addition & 0 deletions doc/23_Installation_and_Upgrade/09_Upgrade_Notes/README.md
Expand Up @@ -11,6 +11,7 @@
- `isMaintenanceModeScheduledForLogin`, `scheduleMaintenanceModeOnLogin`, `unscheduleMaintenanceModeOnLogin` will be removed in Pimcore 12
- [CoreCacheHandler] Remove redundant cache item tagging with own key
- [Auth] The tokens for password reset are now stored in the DB and are one time use only (gets expired whenever a new one is generated or when consumed).
- [Elements] Properties are now only updated in the database with dirty state (when calling `setProperties` or `setProperty`).

## Pimcore 11.0.7
- Putting `null` to the `Pimcore\Model\DataObject\Data::setIndex()` method is deprecated now. Only booleans are allowed.
Expand Down
27 changes: 15 additions & 12 deletions models/Asset.php
Expand Up @@ -732,16 +732,18 @@ protected function update(array $params = []): void

$this->postPersistData();

// save properties
$this->getProperties();
$this->getDao()->deleteAllProperties();
foreach ($this->getProperties() as $property) {
if (!$property->getInherited()) {
$property->setDao(null);
$property->setCid($this->getId());
$property->setCtype('asset');
$property->setCpath($this->getRealFullPath());
$property->save();
if ($this->isFieldDirty('properties')) {
// save properties
$properties = $this->getProperties();
$this->getDao()->deleteAllProperties();
foreach ($properties as $property) {
if (!$property->getInherited()) {
$property->setDao(null);
$property->setCid($this->getId());
$property->setCtype('asset');
$property->setCpath($this->getRealFullPath());
$property->save();
}
}
}

Expand All @@ -752,7 +754,7 @@ protected function update(array $params = []): void

foreach ($this->resolveDependencies() as $requirement) {
if ($requirement['id'] == $this->getId() && $requirement['type'] == 'asset') {
// dont't add a reference to yourself
// don't add a reference to yourself
continue;
} else {
$d->addRequirement($requirement['id'], $requirement['type']);
Expand Down Expand Up @@ -1567,7 +1569,8 @@ protected function resolveDependencies(): array
/** @var DataDefinitionInterface $implementation */
$implementation = $loader->build($elementType);
$dependencies[] = $implementation->resolveDependencies($elementData, $metaData);
} catch (UnsupportedException $e) {
} catch (UnsupportedException) {
//nothing to log here
}
}
}
Expand Down
23 changes: 12 additions & 11 deletions models/Asset/Dao.php
Expand Up @@ -224,15 +224,21 @@ public function updateChildPaths(string $oldPath): array
/**
* Get the properties for the object from database and assign it
*
*
* @throws \Exception
*/
public function getProperties(bool $onlyInherited = false): array
{
$properties = [];

// collect properties via parent - ids
$parentIds = $this->getParentIds();
$propertiesRaw = $this->db->fetchAllAssociative('SELECT * FROM properties WHERE ((cid IN (' . implode(',', $parentIds) . ") AND inheritable = 1) OR cid = ? ) AND ctype='asset'", [$this->model->getId()]);
$propertiesRaw = $this->db->fetchAllAssociative(
'SELECT * FROM properties WHERE
(
(cid IN (' . implode(',', $parentIds) . ") AND inheritable = 1) OR cid = ? )
AND ctype='asset'",
[$this->model->getId()]
);

// because this should be faster than mysql
usort($propertiesRaw, function ($left, $right) {
Expand Down Expand Up @@ -261,18 +267,13 @@ public function getProperties(bool $onlyInherited = false): array
}

$properties[$propertyRaw['name']] = $property;
} catch (\Exception $e) {
Logger::error("can't add property " . $propertyRaw['name'] . ' to asset ' . $this->model->getRealFullPath());
} catch (\Exception) {
Logger::error(
"can't add property " . $propertyRaw['name'] . ' to asset ' . $this->model->getRealFullPath()
);
}
}

// if only inherited then only return it and dont call the setter in the model
if ($onlyInherited) {
return $properties;
}

$this->model->setProperties($properties);

return $properties;
}

Expand Down
26 changes: 14 additions & 12 deletions models/DataObject/AbstractObject.php
Expand Up @@ -704,17 +704,19 @@ protected function update(bool $isUpdate = null, array $params = []): void
{
$this->updateModificationInfos();

// save properties
$this->getProperties();
$this->getDao()->deleteAllProperties();

foreach ($this->getProperties() as $property) {
if (!$property->getInherited()) {
$property->setDao(null);
$property->setCid($this->getId());
$property->setCtype('object');
$property->setCpath($this->getRealFullPath());
$property->save();
if ($this->isFieldDirty('properties')) {
// save properties
$properties = $this->getProperties();
$this->getDao()->deleteAllProperties();

foreach ($properties as $property) {
if (!$property->getInherited()) {
$property->setDao(null);
$property->setCid($this->getId());
$property->setCtype('object');
$property->setCpath($this->getRealFullPath());
$property->save();
}
}
}

Expand All @@ -725,7 +727,7 @@ protected function update(bool $isUpdate = null, array $params = []): void

foreach ($this->resolveDependencies() as $requirement) {
if ($requirement['id'] == $this->getId() && $requirement['type'] === 'object') {
// dont't add a reference to yourself
// don't add a reference to yourself
continue;
}

Expand Down
23 changes: 12 additions & 11 deletions models/DataObject/AbstractObject/Dao.php
Expand Up @@ -222,15 +222,21 @@ public function getVersionCountForUpdate(): int
/**
* Get the properties for the object from database and assign it
*
*
* @throws \Exception
*/
public function getProperties(bool $onlyInherited = false): array
{
$properties = [];

// collect properties via parent - ids
$parentIds = $this->getParentIds();
$propertiesRaw = $this->db->fetchAllAssociative('SELECT name, type, data, cid, inheritable, cpath FROM properties WHERE ((cid IN (' . implode(',', $parentIds) . ") AND inheritable = 1) OR cid = ? ) AND ctype='object'", [$this->model->getId()]);
$propertiesRaw = $this->db->fetchAllAssociative(
'SELECT name, type, data, cid, inheritable, cpath FROM properties WHERE
(
(cid IN (' . implode(',', $parentIds) . ") AND inheritable = 1)
OR cid = ? ) AND ctype='object'",
[$this->model->getId()]
);

// because this should be faster than mysql
usort($propertiesRaw, function ($left, $right) {
Expand Down Expand Up @@ -259,18 +265,13 @@ public function getProperties(bool $onlyInherited = false): array
}

$properties[$propertyRaw['name']] = $property;
} catch (\Exception $e) {
Logger::error("can't add property " . $propertyRaw['name'] . ' to object ' . $this->model->getRealFullPath());
} catch (\Exception) {
Logger::error(
"can't add property " . $propertyRaw['name'] . ' to object ' . $this->model->getRealFullPath()
);
}
}

// if only inherited then only return it and dont call the setter in the model
if ($onlyInherited) {
return $properties;
}

$this->model->setProperties($properties);

return $properties;
}

Expand Down
24 changes: 13 additions & 11 deletions models/Document.php
Expand Up @@ -468,16 +468,18 @@ protected function update(array $params = []): void
$this->setIndex($this->getDao()->getNextIndex());
}

// save properties
$this->getProperties();
$this->getDao()->deleteAllProperties();
foreach ($this->getProperties() as $property) {
if (!$property->getInherited()) {
$property->setDao(null);
$property->setCid($this->getId());
$property->setCtype('document');
$property->setCpath($this->getRealFullPath());
$property->save();
if ($this->isFieldDirty('properties')) {
// save properties
$properties = $this->getProperties();
$this->getDao()->deleteAllProperties();
foreach ($properties as $property) {
if (!$property->getInherited()) {
$property->setDao(null);
$property->setCid($this->getId());
$property->setCtype('document');
$property->setCpath($this->getRealFullPath());
$property->save();
}
}
}

Expand All @@ -488,7 +490,7 @@ protected function update(array $params = []): void

foreach ($this->resolveDependencies() as $requirement) {
if ($requirement['id'] == $this->getId() && $requirement['type'] == 'document') {
// dont't add a reference to yourself
// don't add a reference to yourself
continue;
} else {
$d->addRequirement((int)$requirement['id'], $requirement['type']);
Expand Down
32 changes: 19 additions & 13 deletions models/Document/Dao.php
Expand Up @@ -250,19 +250,30 @@ public function getVersionCountForUpdate(): int
}

/**
* Returns properties for the object from the database and assigns these.
*
* Returns properties for the object from the database
*
* @throws \Exception
*/
public function getProperties(bool $onlyInherited = false, bool $onlyDirect = false): array
{
$properties = [];

if ($onlyDirect) {
$propertiesRaw = $this->db->fetchAllAssociative("SELECT * FROM properties WHERE cid = ? AND ctype='document'", [$this->model->getId()]);
$propertiesRaw =
$this->db->fetchAllAssociative(
"SELECT * FROM properties WHERE cid = ? AND ctype='document'",
[$this->model->getId()]
);
} else {
$parentIds = $this->getParentIds();
$propertiesRaw = $this->db->fetchAllAssociative('SELECT * FROM properties WHERE ((cid IN (' . implode(',', $parentIds) . ") AND inheritable = 1) OR cid = ? ) AND ctype='document'", [$this->model->getId()]);
$propertiesRaw =
$this->db->fetchAllAssociative(
'SELECT * FROM properties WHERE
(
(cid IN (' . implode(',', $parentIds) . ") AND inheritable = 1) OR cid = ?
) AND ctype='document'",
[$this->model->getId()]
);
}

// because this should be faster than mysql
Expand Down Expand Up @@ -295,18 +306,13 @@ public function getProperties(bool $onlyInherited = false, bool $onlyDirect = fa
}

$properties[$propertyRaw['name']] = $property;
} catch (\Exception $e) {
Logger::error("can't add property " . $propertyRaw['name'] . ' to document ' . $this->model->getRealFullPath());
} catch (\Exception) {
Logger::error(
"can't add property " . $propertyRaw['name'] . ' to document ' . $this->model->getRealFullPath()
);
}
}

// if only inherited then only return it and dont call the setter in the model
if ($onlyInherited || $onlyDirect) {
return $properties;
}

$this->model->setProperties($properties);

return $properties;
}

Expand Down
17 changes: 13 additions & 4 deletions models/Element/AbstractElement.php
Expand Up @@ -244,22 +244,29 @@ public function getProperties(): array
Cache::save($properties, $cacheKey, $cacheTags);
}

$this->setProperties($properties);
$this->properties = $properties;
}

return $this->properties;
}

public function setProperties(?array $properties): static
{
$this->markFieldDirty('properties');
$this->properties = $properties;

return $this;
}

public function setProperty(string $name, string $type, mixed $data, bool $inherited = false, bool $inheritable = false): static
public function setProperty(
string $name,
string $type,
mixed $data,
bool $inherited = false,
bool $inheritable = false
): static
{
$this->getProperties();
$properties = $this->getProperties();

$id = $this->getId();
$property = new Model\Property();
Expand All @@ -274,7 +281,9 @@ public function setProperty(string $name, string $type, mixed $data, bool $inher
$property->setInherited($inherited);
$property->setInheritable($inheritable);

$this->properties[$name] = $property;
$properties[$name] = $property;

$this->setProperties($properties);

return $this;
}
Expand Down

0 comments on commit 2ad37be

Please sign in to comment.