mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
Entity Manger only persisting one Entity at a time #5218
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 @eggmatters on GitHub (Aug 16, 2016).
I am writing a series of unit tests on a service. I need entities in various states for different test cases.
I have written methods which accept arrays of values, populates an entity with some values. The first test case that calls these methods operates fine. Subsequent calls fail however with the error:
Undefined index: <some hash value>The test cases are structured thusly:
Expected: Calls to fetchEntity with valid values will persist entity and return persisted entity.
Actual: 'Undefined Index ' is thrown when calling $em->flush().
Stack trace shown is:
@eggmatters commented on GitHub (Aug 17, 2016):
File this one under "good to know";
Unsure of underlying cause but it when I migrated the factory methods to generate entities to a class which provided it's own EM and container, the entities persisted. One could draw an assumption that the entity manager is a separate instance when called. This seems to confuse things when persisting however.
When inspecting the failure in
UnitOfWork.php:2917, A method is looking for an entry in an array keyed by the value from abovearray( '000000006994fd4c0000000020e08b96' => 'id' => <some guid>)There was an entry in the entities array for that id, mapping to the guid I needed but was throwing the 'undefined index' error. Not sure what the function of that array may be.@eggmatters commented on GitHub (Aug 19, 2016):
As a follow up, I've noticed a pattern with this and errors like it. It seems that if you invoke an entity manager to either fetch or generate an Entity and then invoke a new entity manager to persist that entity, you have problems.
In the code above, an EntityManager was invoked to create the object - and a different EM was invoked to persist it. Entity managers should, I guess, be in lockstep with the entities they manage.
@Skemeth commented on GitHub (Mar 22, 2018):
I'm having the same problem today. here's a short version of my code:
The problem happens when the message is not found (second "if" test)
I believe I used the same entity manager to create and persist, so I have no idea how to fix this...?
@Ocramius commented on GitHub (Mar 22, 2018):
Not an ORM issue: please report it here after reproducing in isolation in https://github.com/doctrine/doctrine2/tree/master/tests/Doctrine/Tests/ORM/Functional/Ticket, not with your specific app.