views:

43

answers:

2

What is the best way to store complex models in ZF? In the example below, should each attribute be a separate model entirely, or should the item be a multi dimensional array (as shown below)?

object(Application_Model_Item)#79 (4) {
  ["_id":protected] => int(45)
  ["_name":protected] => string(5) "Bolts"
  ["_description":protected] => NULL
  ["_attributes":protected] => array(2) {
    [0] => array(2) {
      ["id"] => string(1) "3"
      ["name"] => string(4) "Size"
    }
    [1] => array(2) {
      ["id"] => string(1) "4"
      ["name"] => string(6) "Length"
    }
  }

Thanks in advance.

A: 

It all depends on your use case:

Indexing by ID or Position:

  • If you would like speed when accessing a particular attribute, then index the attributes by their IDs instead of their index position.
  • If you would like to keep an order, then order them by index position and a position offset amount.

Independent Table Vs Local Array:

  • If the attributes are duplicated in multiple items, then have them as their own table, and reference the attributes to that table.
  • If the attributes are not refenced and are unique to each item, then using them as serialise-able arrays (for storage) is adequate than needing them to be their own table.
balupton
Thanks for the reply. I really should take a look at doctrine it's just I'm too lazy to learn a new language. I've also heard doctrine can become quite resource intensive so I was planning on coding the mappers myself (in php).
Ben Muncey
No worries, hope I helped :-)Doctrine is actually fairly simple to learn, I moved to it after my own ORM was getting a bit too legacy. It is quite resource intensive and difficult to debug at times, but I don't regret the move as the downfalls are entirely reasonable as well.
balupton
A: 

In the case of _attributes i would use array of objects. So it attributes would be an array of new model Attribute()

I make a class for every business model entity

["_attributes":protected] => array(2) {
    [0] => Object(Model_Attribute) {}
    [1] => Object(Model_Attribute) {}
}

class Model_Attribute {
  protected $id;
  public function getId();
  public function setId($id);
  .
  .
  .
}

I suggest you look at Doctrine ORM 2.0 since it can support the design from the above. Look at this page, it may give you a clue: http://www.doctrine-project.org/projects/orm/2.0/docs/reference/association-mapping/en

Dr Casper Black