Tiered? structure of complex Zend Framework models/objects
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"
}
}
开发者_如何学Python
Thanks in advance.
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.
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
精彩评论