#36102In 1:n relationships the parent might get lost
Description
Given two classes that have a 1:n relationship modelled by means of a parent field in the child table like this:
class Parent {
/**
* @var Tx_Extbase_Persistence_ObjectStorage<Child>
*/
public $children;
}
class Child {
/**
* @var Parent
*/
public $parent;
}
Updating the relationship can result in the child being stripped of its parent.
$newParent = $parentRepository->...; $child = $childRepository->...; $child->parent->children->remove($child), $newParent->children->add($child); $child->parent = $newParent;
This code sequence gives a consistent state in memory, but during persistAll(), extbase will set the child’s parent to NULL.
This is because extbase tries to be smart and to fix the parent field in case that the user didn’t fully update the involved parties, but just added the child to its new parent.
In the above scenario, the objects are saved in this order$newParent$child$oldParent
- In
persistObjectStorage(), called for$newParentthe parent field gets updated to the new parent uid. - In
persistObject()for$childthe parent field gets updated to the new parent uid again. - In
persistObjectStorage()for$oldParent, the child is stripped of its parent by callingdetachObjectFromParentObject()
Thus the child becomes orphaned.
This can be worked around by means of calling persistAll() yourself, because of another bug that causes ObjectStorages not to be marked as clean, thus they get persisted again when extbase calls persistAll() itself, but this time the detaching won’t happen, because the clean state of the parent object has been updated.
Reviews 0
No change resolves or names this issue in its commit message, and no comment links one.
History 10
-
Marc Bastian Heinrichs commented 04/16/12, 17:40
-
Björn Steinbrink commented 04/17/12, 09:33
'parent' => array( 'config' => array( 'type' => 'select', 'foreign_table' => 'tx_extension_domain_model_parent', 'foreign_class' => 'Tx_Extension_Domain_Model_Parent', 'minitems' => 0, 'maxitems' => 1, ) )and
'children' => array( 'exclude' => 1, 'config' => array( 'type' => 'inline', 'multiple' => 1, 'foreign_class' => 'Tx_Extension_Domain_Model_Child', 'foreign_table' => 'tx_extension_domain_model_child', 'foreign_field' => 'parent', 'foreign_sortby' => 'sorting', ) )That the parent field is accessible doesn’t even matter, it also fails if you just update the object storages for the children in the old and new parent, if things get persisted in the “wrong” order (the new parent is persisted first).
The use case is that I’m moving a child from one parent to another, that means that I either directly replace the parent, or that I first remove the parent and then set the new one. But extbase might as well end up first setting the new parent and then removing it.
A possible workaround might be to check that the child still has the old parent, and only unset its parent if that is the case, i.e. generate an update statement like
UPDATE tx_extension_domain_model_child SET parent = NULL WHERE parent = 123
But that could lead to other weird bugs in other cases, like this:
$oldParent->remove(child); $newParent->add(child); // somewhere else $child->setParent($oldParent);
If
$oldParentgets persisted last, the$childwould still end up without a parent.Key in this example is that the in-memory state is inconsistent again. The two directions of the relationships don’t match. IMHO extbase should just require that relationships are kept consistent in-memory (i.e. always update both sides), and then just persist the “parent” field, without trying to be smart. Having to keep it all in sync is the price one pays for using an object model instead of relational data.
-
Alexander Schnitzler commented and changed assigned to Alexander Schnitzler target version 11/18/12, 18:32
I guess we have to check if we have to rewrite the persistAll method and I guess we will not make it before 6.1.
-
Alexander Schnitzler changed status
NewNeeds Feedback 04/12/13, 14:45 -
Alexander Schnitzler changed target version 04/23/13, 23:01
-
Anja Leichsenring changed target version 05/31/13, 15:28
-
Alexander Schnitzler changed removed the assignee 01/13/14, 20:39
-
Alexander Opitz changed project id category status
Needs FeedbackNew target version TYPO3 Version 6.2 Is Regression 0 09/15/14, 14:32 -
Mathias Schreiber changed category 11/12/15, 20:15
-
Christian Kuhn commented and changed status
NewClosed 04/23/26, 11:41Hey. I hope it is ok to close here after ten years without anything happening. Let’s restart fresh in case this is still an issue.
Could you also post the tca of Parent children and Child parent, please?
It sounds more like an not supported relation type in T3 core than a bug in extbase.