views:

156

answers:

2

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.

+2  A: 

First I think the title of the question isn't very clear, second I'm not entirely sure I understand what is being asked... In my opinion both the title and the post really should be edited to clean it up a bit.

The post seems to contain 5 questions (if I might paraphrase):

  1. How would one go about retrieving a dependent/parent object from an entity?
  2. Is $person->getAddress() a good way to retrieve the Address object for a person from the database?
  3. If this a good way to do it then what would the code look like?
  4. Is it okay to use the $domain object inside the getAddress method?
  5. Is it okay to keep relation information in the Person class?

As far as answers go:

  1. In this case $person->getAddress() seems a good choice, you know, nice and descriptive. ;-)
  2. I don't see a problem: Person HAS A Address, to get that adress you ask the person object for is.
  3. The given code example seems to answer this, although it is a bit cluttered. I would've just made address a member object, but other than that it's mostly the same
  4. & 5. I'd say are mostly a matter of taste, priority and design-dependency. There really isn't a clean cut answer to this. Also, are we arguing semantics here or a real life scenario?

Some code to go along with answer Nr.3: (I couldn't get it to work properly within the list)

class Person {
    protected $_domain = null; // domain is assigned when instantiated
    protected $_address;        // object

    public function getAddress() {
        if (!isset($this->_address) ) {
            $this->_address = $this->_domain->getAddressForPerson($this); // assuming $this contains some sort of ID
        }
        return $this->_address;
    }
}

Hope this helps.

Ben Peachey
+3  A: 

Take a look at Zend_Db_Table Relationships. They allow doing exactly that, and much more. You can just use Zend_Db_Table today, instead of trying to come up with your own implementation, or you can study Zend Framework's code for their design decisions wrt to your questions. It's quite an authoritative source.

Ivan Krechetov
Thanks, but I have been using Zend_Db_Table for a while and they don't help me abstract the domain as easily as I can with Outlet PHP or other ORMs.
andho
What gets in your way with Zend_Db_Table andho? Your question sounds to me completely covered with Zend_Db_Table functionality. And you can always add more to your table subclasses. I don't think it's worth to opt for your own solution from scratch.
Ivan Krechetov
I agree with Ivan. While I don't agree with the entire implementation of Zend_Db_Table, this is one of the areas it really accels at in my option.
Kevin Peno