Need clarification about nested models

rineIndiste

New Member
I need an example on how to operate on nested models.Let's assume, we have some order management application and models: Order, Position, Item.Order can contain Positions and Positions can contain Items.The question is: how to handle data selection?Some use cases that is needed:
  • Get list of Orders with Positions andItems (like in my example code)
  • Get only list of Orders (without Positions and Items)
  • Get some statistics, for example, total amount of all Items in single Position
I can do something like that:\[code\]class Order_Mapper{ public function getOrders($criteria) { $orders = array(); // Get order list from DB foreach($orders as $order) { $positions = Position_Mapper::getPositionsByOrderId($order->id); foreach($positions as $position) { $order->addPosition($position); } } return $orders; }}class Position_Mapper{ public function getPositionsByOrderId($order_id) { $positions = array(); // Get position list from DB foreach($positions as $position) { $items = Item_Mapper::getItemsByPositionId($position->id); foreach($items as $item) { $position->addItem($item); } } return $positions; }}\[/code\]... but this is inefficient because there is many SQL queries needed, one for order select, then one for each Order to get Positions and then one for each Position to get Items.I'm pretty inexperienced in MVC so any help is welcome!In response about Doctrine - I really didn't want to learn new ORM suite, nor I don't mind writing SQL queries by hand. I just need the idea how to accomplish aforementioned tasks.Here is my second try to do all of the use cases mentioned above without overhead. Please, take a look and tell me your thoughts. Code is very simple.
 
Back
Top