When creating an association using Doctrine 2 and the Zend Framework, if the associated object is empty e.g. for entity->associated_entity->item if associated_entity is empty, i.e. there is not an associated entity to the original entity, then I get an error Trying to get property of non-object.
I know this is because I am trying to get the item from an empty entity.
What is the standard way to avoid this error?
I am using the code below to get the data, but because the initial associated entity will be returned as ”, then it can’t then get the item from ”
public function __get($name)
{
if (isset($this->$name)){
return $this->$name;
} else {
return '';
}
}
you could try:
Edit:
OK then. Try putting this in your template/view:
Edit 2 (after a small discussion in the chat :D):
I think that there is no way to tell to PHP to stop the chain. E.g.
$object1->object2->attributeIf you write it this way, no matter what you put in the __get(), PHP will assume that object2 is an object and will try to fetch the requested attribute.
The easiest solution would be something like that: