i have a question about the correct way to handle doctrine's lazy loading mechanism. I have an entity that references another one via @ManyToOne:
class Entity {
...
/**
* @ManyToOne(targetEntity="AnotherEntity")
* @JoinColumn(name="anotherEntityId", referencedColumnName="id")
*/
protected $anotherEntity;
...
}
class AnotherEntity {
/**
* @var integer $id
* @Column(name="id", type="integer", nullable=false)
* @Id
* @GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string $someValue
* @Column(name="someValue", type="string")
*/
private $someValue;
...
}
Doctrine now generates a proxy for AnotherEntity that implements lazy loading on its getters:
public function getSomeValue()
{
$this->__load();
return parent::getSomeValue();
}
If I now have some instances of Entity that do not have a reference counterpart of AnotherEntity ($anotherEntityId is 0 or null). Then doctrine seems to generate ONE null-Object of AnotherEntity and makes the $anotherEntity variables of all Entity object point to it. The null-object is uninitialized since we want lazy loading. I now get an EntityNotFoundException when I do this:
$entiy->getAnotherEntity()->getSomeValue();
That's great! The AnotherEntity I access does not exist in the database and so I get this exception. BUT when I do the same with the second Entity (and all others afterwards) I do not get this exception, since they are all pointing to the same instance of AnotherEntity and doctrine marked it as initialized in the first call. So now I'm accessing an object that does not exist logically but do not get any exception - I just get the uninitialized variable from getSomeValue(), e.g. an empty string.
Do you have any idea how I can make my application to recognize that the object does not exist instead of just accepting the empty value?