Situation:
I want to use the getReference() function of doctrine2 Entity Manager. However, in a situation where I ask for an object that has been deleted from the database, I obtain a proxy if I ask for that same object more than once.
An example:
//A random article object...that has been deleted from the database
$articleClass = 'Acme\ArticleBundle\Entity\Article';
$articleIdentifiers = array('id'=>1);
$i = 0;
//We ask for its reference twice
do{
try {
echo "a";
$subject = $this->em->getReference(
$subjectClass,
$subjectIdentifiers
);
//call this object now
var_dump($subject);
} catch (\Exception $e) {
echo "b";
}
$i++;
} while ($i <2);
The output:
a
b
a
object(Proxies\__CG__\Acme\ArticleBundle\Entity\Article)
How can I get a proxy for an object that doesn't even exist in the database? If I comment this line, the entityManager does not manage the object and I obtain the output abab
, which to me makes more sense as I don't want to get a proxy object that does not exist in the database. For info, the proxy object returned has all its properties null
. I therefore obtain a proxy for an object that does not exist in the database. So, if I ask for this object I get a "Not found Entity" exception.
The challenge
Can anyone make any sense of this? Is there a way to rely on getReference()
to tell us whether this object really does exist in the database?