Is there a way to determine if a parameter is an object that is already persisted by Doctrine or not? Something like an entity manager method that checks that an object is not a plain old object but actually something already in memory/persisted.
<?php
public function updateStatus(Entity $entity, EntityStatus $entityStatus)
{
$entityManager = $this->getEntityManager();
try {
// checking persisted entity
if (!$entityManager->isPersisted($entity)) {
throw new InvalidArgumentException('Entity is not persisted');
}
// ...
} catch (InvalidArgumentException $e) {
}
}
EDIT: As said by @Andrew Atkinson, it seems
is the preferred way now.
Previous answer: You have to use UnitOfWork api like this:
An Easier and more robust way to check if the entity is flushed or not, just check for the ID.
This solution is very much dependant on the case, but it may be the best for you
edit:
From comments it appears this is not the most relevant answer, however may provide useful for someone as it is closely related to the question.
The
EntityManager
methodcontains
serves this purpose. See the documentation (2.4).In Doctrine 2.4, the implementation looks like this: