views:

66

answers:

2

I have two tables - let's call them Customers and Orders. It's a 1:n relationship, where each Order is placed by one Customer, and one Customer can have many orders.

The Web app is written in Zend Framework. My goal is to query a customer from the database, and get their 10 most recent orders (for example), and to have all this information bundled into a single object that can be passed between several controllers/views/helpers. I want to do all the querying up-front in the initial controller, and then create a single object (or whatever) that can be handed from code to code, with no further querying.

I'd also like to minimize querying as much as possible in that initial step. That is, I know one way would be to query the desired customers, enumerate through them, and construct a custom object (or just an array) containing their values. During that enumeration, I'd query each customer's orders and tack that info on to each customer object. I'd then pass the collection of customer objects around.

Short of that rather brute-force approach, is there a better approach for this?

A: 

You could implement some kind of caching using memcached, and Zend_Cache, that way you can have the 10 most recent orders, and in any event you insert a new one you just delete the cache for that customer and re-populate it.

Chris
Hmm, thanks. I was really hoping to be able to build some sort of hierarchical, nested object; it's looking like the answer is to construct that myself based on multiple queries, and then pass it around. Thanks!
Don Jones
You can store that kind of objects in memory too, you can just serialize and create a controller plugin to serialize and unserialize it, so you can have it as a member of your controller.
Chris
A: 

Maybe you should look into using an ORM layer, ie. Propel or Doctrine. You can mix those with Zend Framework apps as well (I'm using Propel at the moment). These will return your PHP objects, so you can use them directly as you see fit.

wimvds