Bertuz
Bertuz

Reputation: 2566

doctrine does not keep the entity state when finding it again

Within a single Unit Of Work I'm trying to do the following:

What I get is a managed state in place of a removed one. Is that correct? I'm not finding any piece of information about this behavior.

EDIT

    $this->em->remove($varSelection);
    $en = $this->em->getRepository(VarSelection::class)->findOneById($varSelection->getId());
    dump($varSelection === $en //FALSE

$varSelection returns a state of Remove, while $en not. Plus, they are not the same object.

According to the Doctrine's documentation, the “Identity Map” pattern should do the job, but apparently it does not.

Upvotes: 0

Views: 569

Answers (1)

DonCallisto
DonCallisto

Reputation: 29912

If you take a look to the internals, you can notice this

public function scheduleForDelete($entity)
{
    $oid = spl_object_hash($entity);

    if (isset($this->entityInsertions[$oid])) {
        if ($this->isInIdentityMap($entity)) {
            $this->removeFromIdentityMap($entity);
        }
    [...]
}

This is called during remove operation, at some point. As you can understand, your object is no longer into the IdentityMap so next findById will retrieve a brand new object from db.

However IdentityMap works as you expect in general: if you query for an object (by id) that was inserted into IdentityMap before, you'll get back the same object; after a remove the behavior is what you noticed.

Honestly I don't know that much about Doctrine internals to explain why remove operation removes entity from IdentityMap but to answer your question

What I get is a managed state in place of a removed one. Is that correct?

yes, this seems to be the correct behavior.

Upvotes: 1

Related Questions