How would one go about retrieving a dependent/parent object from an entity.
$person->getAddress();
This should retrieve the Address object for that person from the database and return it as an object. Is this a good way to do it and how should the code look like if this is ok to do.
Doing this would mean that the object itself should be aware that address is an entity/value object that it is related. Why i want this kind of syntax is because it will look clean in the presentation layer.
The person class would look like this:
class Person {
protected $_domain = null; // domain is assigned when instantiated
protected $_data = array('name', 'address');
protected $_relations = array(
'address'=>array(
'class'=>'Address'
)
);
protected $_retrievedRelations = array();
public function getAddress() {
if (array_key_exists('address', $this->_relations) ) {
if (!array_key_exists('address', $this->_retrievedRelations) ) {
$this->_retrievedRelations['address'] = $this->_domain->getAddress($this->_data['address']);
}
return $this->_retrievedRelations['address'];
}
return $this->_data['address'];
}
}
So is it ok to use the $domain object inside the getAddress method and to keep relation information in the Person class?
Please answer because i've been looking all over for an answer.