mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
Using Proxies obtained through EntityManager::getReference() in associations? #6934
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 @mpdude on GitHub (Feb 21, 2022).
I have two entity classes,
CartandItem, whereCarthas a many-to-many association withItem.Inside a functional test, I'd like to shortcut things a bit, and thus I obtain a reference (proxy) for
ItemthroughEntityManager::getReference(Item::class, 42).I then create a new
Cartinstance, and the constructor will initialize theCart::$containedItemscollection as a newArrayCollection. To this collection, I add theCartreference.When I try to persist the
Cart, I get an exception stating thatItemwas found as a new Entity through the relationshipCart::$containedItemsthat was not configured to cascade persist operations.I know I am on thin ice here, but the documentation on Reference Proxies states:
Details of what's happening
When the data is flushed,
UnitOfWork::computeAssociationChanges()will look at thecontainedItemsassociation. It iterates over the contained entities and tries to determine their state:193c3abf0e/lib/Doctrine/ORM/UnitOfWork.php (L914-L919)UnitOfWorkdoes not have a state for the reference/proxy object in its::$entityStatesarray, and thus returnsSTATE_NEW. This leads to taking note of the allegedly new entity a few lines later:193c3abf0e/lib/Doctrine/ORM/UnitOfWork.php (L930-L939)Nothing around that area tries to recognize
$entryas a proxy. When I try to skipinstanceof Proxyvalues in that place, we will later get intoUnitOfWork::getEntityIdentifier()with the proxy as$entity.This method, again, is not prepared to deal with a proxy:
193c3abf0e/lib/Doctrine/ORM/UnitOfWork.php (L3079-L3083)The proxy is not in the
::$entityIdentifiersarray. It carries the values we'd expect to find in that array in its fields, but my guess would be we'd need a suitableProxyDefinitionto obtain them.Applicable test cases
I have found
\Doctrine\Tests\ORM\Functional\BasicFunctionalTest::testSetSetAssociationWithGetReferencewhich suggests to test the use case I described initially. This test, however, notes:193c3abf0e/tests/Doctrine/Tests/ORM/Functional/BasicFunctionalTest.php (L547-L551)That makes me believe the test never really worked with an uninitialized proxy. When the proxy is initialized by accessing properties, we probably have a fully loaded, state-tracked entity with known identifier values in the
UnitOfWork.So, first question probably: Is this accepted as a bug and something that would get support being fixed?
@mpdude commented on GitHub (Jun 25, 2023):
I seem to be unable to reproduce this as of today.
Test code: