I'm trying to get some clarification on http://www.doctrine-project.org/jira/browse/DDC-136 .
If I create an object in a limited scope and then persist+flush that object it should get saved into the storage engine. If the code execution leaves that scope the object gets destroyed by PHP garbage collection. Consequently, another new object can share the same spl_object_hash, if that new object is persisted+flushed then this can cause issues with Doctrine, assuming that the same instance of Doctrine is available in both scopes (in this case, via Symfony2 service container).
It would seem that this is caused because $this->documentStates in UnitOfWork.php does not (and could not) get updated when the first object is destroyed by garbage collection. That array maintains the object state of the previous object that shared the same hash, thus the wrong queries get executed against the storage engine when a second object with the same hash is processed.
Are you saying that this is the correct behavior?
DDC-136 suggests that detach() should be called for each object just before it goes out of scope, this does seem to fix the issue. However, it doesn't seem to be documented. I don't see how a user could otherwise be expected to know about this behavior, the description at http://doctrine-orm.readthedocs.org/en/2.0.x/reference/working-with-objects.html#detaching-entities gives no explanation of why detach() should be called, merely the effects of doing so which do not make it clear that it is ever necessary.
Personally I feel that spl_object_hash() is an inappropriate mechanism to use for this, however in the absence of any better suggestion it would be nice if the documentation reflected some of the quirks that should be expected.