DDC-2624: ManyToManyPersister fails to handle cloned PeristentCollections #3293

Open
opened 2026-01-22 14:17:32 +01:00 by admin · 0 comments
Owner

Originally created by @doctrinebot on GitHub (Aug 20, 2013).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user bountin:

I want to clone an entity and persist the clone. The entity itself works (if I reset the identifiers to null) but a M2M collection was first ignored since I only did a shallow copy. When I do a deep copy with the following, Doctrine throws the following exception:

public function _clone()
{
if ($this->question_versions instanceof PersistentCollection) {
$this->question_versions = clone $this->question
versions;
}
}

Fatal error: Uncaught exception 'Doctrine\Common\Persistence\Mapping\MappingException' with message 'The class 'Doctrine\ORM\Persisters\ManyToManyPersister' was not found in the chain configured namespaces Foo\Entity' in /var/www/foo/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/MappingException.php on line 37

I've traced the error to the ManyToManyPersister class at the methods get{Delete,Insert}RowSQL where $coll->getOwner() is called which returns null because the owner is cleared when the collection is cloned. Therefore get_class does not work as expected under this circumstances.

I've also tried to use $coll->getTypeClass() for $class at that point but this leads to other warnings ("array key not existing" and "spl_object hash got null") and finally an SQL exception because Doctrine is inserting null as one of the identifiers.

There is a workaround for this issue but I think that this edge case should be handled too. The workaround is not to clone the collection itself but only copy the values with getValues() and let Doctrine convert it back to a collection.

Originally created by @doctrinebot on GitHub (Aug 20, 2013). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user bountin: I want to clone an entity and persist the clone. The entity itself works (if I reset the identifiers to null) but a M2M collection was first ignored since I only did a shallow copy. When I do a deep copy with the following, Doctrine throws the following exception: public function *_clone() { if ($this->question_versions instanceof PersistentCollection) { $this->question_versions = clone $this->question*versions; } } Fatal error: Uncaught exception 'Doctrine\Common\Persistence\Mapping\MappingException' with message 'The class 'Doctrine\ORM\Persisters\ManyToManyPersister' was not found in the chain configured namespaces Foo\Entity' in /var/www/foo/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/MappingException.php on line 37 I've traced the error to the ManyToManyPersister class at the methods get{Delete,Insert}RowSQL where $coll->getOwner() is called which returns null because the owner is cleared when the collection is cloned. Therefore get_class does not work as expected under this circumstances. I've also tried to use $coll->getTypeClass() for $class at that point but this leads to other warnings ("array key not existing" and "spl_object hash got null") and finally an SQL exception because Doctrine is inserting null as one of the identifiers. There is a workaround for this issue but I think that this edge case should be handled too. The workaround is not to clone the collection itself but only copy the values with getValues() and let Doctrine convert it back to a collection.
admin added the Bug label 2026-01-22 14:17:32 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#3293