mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
DDC-3947: Issue in collections with Doctrine 2.5 & Symfony 2.6 #4827
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @doctrinebot on GitHub (Oct 10, 2015).
Originally assigned to: @beberlei on GitHub.
Jira issue originally created by user Keen:
Hey Doctrine guys,
Here is a bug report I've made a few weeks ago to Symfony project but with no response link https://github.com/symfony/symfony/issues/15797. I don't really know which team should correct the issue so I share it with you too.
After upgrading Doctrine from 2.4 to 2.5, I've been facing a new problem with my collections in Symfony which were not working well when I tried to add/remove an item from my collection.
The entity association is simple : OneToMany; in my case one User linked to many Skills.
At first sight, this message appeared " 'spl_object_hash() expects parameter 1 to be object, null given' " with no reason.
Here is the stack trace :
in vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php at line 2445 -
After looking at the Doctrine and Symfony code, I think I've spotted the problem.
If my understandings are good, when working with collections, Symfony has to prepare the field of the data_class object to receive potential new elements. This preparation is done at PRE_BIND state by the method "readPropertiesUntil" of PropertyAccessor class (line 191) by initializing new column with "null" value.
As we can see, the missing columns in the array are added thanks to this line :
$objectOrArray[$property] = $i 1 < $propertyPath->getLength() ? array() : null;
As Doctrine uses ArrayAccess implementation for the PersistentCollection, the use of $objectOrArray[$property] is equivalent to a call to the offsetSet method located at line 522 in the PersistentCollection class.
Then a call to the set method is issued :
And in my opinion, the problem comes here.
In Doctrine 2.4, the set method was :
Between these two versions, we can see the introduction of this piece of code :
And cancelOrphanRemoval results in :
So, when the PropertyAccessor initializes the object to prepare it to receive the new value which will be inserted during the write phase, it issues unset($this->orphanRemovals[spl_object_hash(null)]) indirectly through the cancelOrphanRemoval method. That's the reason why the exception is produced. This call was not issued in Doctrine 2.4 which explains why there was no problem.
As a quick workaround, I've used this
because I don't know if it's the role of Doctrine to restrict null values or the role of Symfony to initialize the PersistentCollection in another way.
Sorry for this very long bugreport,
Keen
@beberlei commented on GitHub (Feb 16, 2020):
Tricky, not sure thta calling set with a
$value === nullshould be allowed, for me its a problem with Symfony Forms. But adding the workaround also makes sense, from a temporary perspective.