Breaking: #111136 - Extbase entity dirty tracking methods removed
See forge#111136
Description
Extbase kept the original ("clean") property values of a domain object inside the object itself and asked the object whether it had been modified. This dirty tracking has moved from the entity into the persistence layer, in line with the concepts of the Doctrine ORM, where the original entity data and the change set are managed outside of the entity.
The following methods have been removed from
\TYPO3\:
_memorizeClean State () _memorizeProperty Clean State () _getClean Properties () _getClean Property () _isDirty () _isClone () _setClone () __clone () - the protected method
isProperty Dirty ()
The method
_get has also been removed from
\TYPO3\.
The interface
\TYPO3\
has been removed.
\TYPO3\
and
\TYPO3\ no longer implement it,
which removes
_memorize and
_is from
Object as well. The storage still tracks internally whether it
has been modified.
Domain objects are not flagged as clones anymore, a clone is an ordinary object without any special state.
The replacement is the service
\TYPO3\, which the
persistence layer uses itself. It keeps the original values of every object
that the persistence layer has loaded or inserted, and forgets them when the
object is garbage collected. The original values are not part of the entity
anymore, so they are neither copied by
clone
nor contained in a
serialized entity. An object that has a uid, but that the persistence layer has
neither loaded nor inserted, is not managed: This is the case for an
unserialized or a cloned entity, and for an object that has been built by hand
and given a uid.
Important
The
Clean is marked as
@internal
. Its public
methods are offered as the replacement for the removed entity methods in
TYPO3 v15 only. It has no stability promise beyond that: its shape may
change in TYPO3 v16 without a further Breaking changelog entry.
Impact
Calling one of the removed methods on a domain object or an
Object results in a PHP error. A class that implements
Object fails to load. Code that overrides the
protected method
is is not called anymore.
A domain object that overrides
__ and calls
parent::__ fails with a PHP
Error
, as the parent class
does not define the method anymore:
Call to undefined method
\.
Objects that implement
Domain without extending
Abstract are not tracked anymore. The
Clean does not register original values for them, so they
have no change set, and an update writes none of their properties. Extend
\TYPO3\ or
\TYPO3\ instead.
Passing an object that is not managed to
\TYPO3\,
\TYPO3\ or the persistence
manager throws a
\TYPO3\,
which extends
Unknown. The same exception is thrown when
the persistence layer reaches such an object in the relations of an object that
is persisted, for example a cloned child that is attached to a managed entity.
Nothing is written in this case. Before, such an object was written like a
managed one, which overwrote the record with all values of the object and added
the rows of its relations a second time.
Reading an unserialized or a cloned entity still works. It has no original values, so the tracker reports it as not changed.
The form ViewHelpers no longer render the HTML comment
<!-- Object of type ... is without identity -->
for a clone of a new
object, as clones are not flagged anymore. A new object, cloned or not, renders
no identity field.
Affected installations
Installations with extensions that
- call one of the removed methods on domain objects or on an
Object, for example to find out whether the user changed something in an edit form,Storage - implement
Object, orMonitoring Interface - override
isin a domain object,Property Dirty () - override
__in a domain object and callclone () parent::__,clone () - implement
Domainwithout extendingObject Interface Abstract,Domain Object - store entities serialized in a session or a cache, for example the state of a multi-step form, and update them later, or
- update a clone of an entity.
The extension scanner reports calls of the removed methods as weak matches, as
it only matches the method names and cannot tell which class a method is called
on. The interface
Object is reported as a strong
match. A call of
parent::__ is not reported, as the scanner
cannot resolve
parent::
calls.
Migration
Entities that are stored and updated later
Do not store an entity that is updated later. A multi-step form keeps the submitted values together with the identity of the entity, and lets the property mapper convert them to the managed entity in the last step:
// Before: the entity is part of the form state and updated in the last step
$formState['blog'] = $blog;
// ...
$this->blogRepository->update($formState['blog']);
// After: the identity and the submitted values are part of the form state
$formState['blog'] = ['__identity' => $blog->getUid(), 'title' => $title];
// ...
$configuration = new PropertyMappingConfiguration();
$configuration->allowAllProperties();
$configuration->setTypeConverterOption(
PersistentObjectConverter::class,
PersistentObjectConverter::CONFIGURATION_MODIFICATION_ALLOWED,
true
);
$blog = $this->propertyMapper->convert($formState['blog'], Blog::class, $configuration);
$this->blogRepository->update($blog);
Cache or store the uid, or the data, instead of an entity that is updated later, and fetch the entity from the repository when it is needed:
// Before
$cache->set($cacheIdentifier, $blog);
// ...
$blog = $cache->get($cacheIdentifier);
$blog->setTitle($title);
$this->blogRepository->update($blog);
// After
$cache->set($cacheIdentifier, $blog->getUid());
// ...
$blog = $this->blogRepository->findByUid($cache->get($cacheIdentifier));
$blog->setTitle($title);
$this->blogRepository->update($blog);
Change the loaded object instead of a clone. To copy a record, build a new instance and add it, instead of cloning and updating the entity:
// Before: the clone has the uid of the original and is updated
$copy = clone $blog;
$copy->setTitle($title);
$this->blogRepository->update($copy);
// After: change the loaded object
$blog->setTitle($title);
$this->blogRepository->update($blog);
// After: copy the record
$copy = new Blog();
$copy->setTitle($title);
$this->blogRepository->add($copy);
Entities that are serialized and unserialized after this change are not managed. An entity that was serialized before this change contains the former original values. They are ignored when it is unserialized, for example when a form state of a running multi-step form is restored during an update.
Domain objects that override __clone()
Remove the call of
parent::__. Resetting the uid in the own
__ stays possible. The clone then is a new object that can be
passed to
add:
// Before
public function __clone()
{
parent::__clone();
$this->uid = null;
}
// After
public function __clone()
{
$this->uid = null;
}
Dirty checks
Inject
\TYPO3\ and ask
it instead of the domain object:
$object->_becomesis Dirty () $cleanState Tracker->is Entity Dirty ($object) $object->_becomesis Dirty ($property Name) $cleanState Tracker->is Entity Property Dirty ($object, $property Name) $objectbecomesStorage->_ is Dirty () $cleanState Tracker->is Entity Dirty ($object Storage) $object->_becomesget Clean Properties () $cleanState Tracker->get Original Entity Data ($object) $object->_becomesget Clean Property ($property Name) $cleanState Tracker->get Original Entity Data ($object) [$property Name] ?? null $object->_andmemorize Clean State () $objectbecomeStorage->_ memorize Clean State () $cleanState Tracker->set Original Entity Data ($object) $object->_becomesmemorize Property Clean State ($property Name) $cleanState Tracker->set Original Entity Property ($object, $property Name) - the changed properties with their original and current value are available
with
$cleanState Tracker->get Entity Change Set ($object) - whether the persistence layer manages an object is answered by
$cleanState Tracker->contains ($object)
Code that is not built by the container can get the service with
General.
Ask whether anything has been changed, for a single property and for a to-many relation:
// Before
if ($object->_isDirty($propertyName)) {
// ...
}
if ($object->getImages()->_isDirty()) {
// ...
}
// After
public function __construct(
private readonly CleanStateTracker $cleanStateTracker,
) {}
if ($this->cleanStateTracker->isEntityPropertyDirty($object, $propertyName)) {
// ...
}
if ($this->cleanStateTracker->isEntityDirty($object->getImages())) {
// ...
}
Handle objects that are new or have been changed, for example for a validation:
// Before
if ($object->_isNew() || $object->_isDirty()) {
// ...
}
// After
if ($object->getUid() === null || $this->cleanStateTracker->isEntityDirty($object)) {
// ...
}
A dirty check is not a way to find out whether an object has been persisted. A new object has no original data and is never dirty, so the negated check does not tell new objects from persisted ones. Ask the object itself:
// Before
if (!$object->_isDirty()) {
// not yet persisted
}
// After
if ($object->getUid() === null) {
// not yet persisted
}
The check for a new object that is not a clone is reduced to the check for a new object:
// Before
if ($object->_isNew() && !$object->_isClone()) {
// ...
}
// After
if ($object->getUid() === null) {
// ...
}