mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
Wrong ORMInvalidArgumentException "A new entity was found through the relationship ..." #5171
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 @insel-maz on GitHub (Jun 30, 2016).
Test provided here
An association entity (
OrderLine) is added to both sides (ShopOrder#orderLinesandArticle#orderLines).ShopOrderandOrderLineform an aggregate.ShopOrderis the aggregate root and thereforeShopOrder#orderLinescascades all operations.Articleis not part of the aggregate andArticle#orderLinesis a simple collection without cascading.Because the new
OrderLineis added toArticle#orderLinesDoctrine throws aDoctrine\ORM\ORMInvalidArgumentExceptionwhich should not occur because the same entity is also added to the second collectionShopOrder#orderLines.@Ocramius commented on GitHub (Jul 5, 2016):
I just checked your example, and it seems like something is wrong on the consumer side:
7a2288d18a/index.php (L22)Copying the code for reference:
You are not calling
$entityManager->persist($shopOrder1), so the ORM doesn't perform any cascade persist operations on that side. Seems like expected behavior to me, at least...@Ocramius commented on GitHub (Jul 5, 2016):
Tentatively marked as
Invalid, although I'd first want to hear what the expected behavior would be here.Also, can you try running a
clear()andfind()in between, and see if it works?@insel-maz commented on GitHub (Jul 5, 2016):
In section 'Creating order with article...' there is a
$entityManager->persist($shopOrder1). After this point the entity is managed and the UnitOfWork will check for changes for every further call toflush().In the last section 'Adding some more articles...' the error occurs: A new
OrderLineis created and added to two collections. Then while flushing it seemsArticle#orderLinesis checked first. Doctrine prematurely throws an exception ignoring the second collectionShopOrder#orderLineswith cascading enabled.Clearing and finding the entities in the beginning of the last section results in the same error.
@DHager commented on GitHub (Nov 8, 2016):
@insel-maz Does the fix in PR #1521 solve your scenario?
@insel-maz commented on GitHub (Nov 10, 2016):
@DHager Your updated
UnitOfWork.phpfixes the problem, thank you.@teohhanhui commented on GitHub (Mar 30, 2017):
Got bitten by this too. My scenario is:
Payment -(cascade persists)-> PayAction
RedeemUserCreditsAction -> PayAction