Note that there are some explanatory texts on larger screens.

plurals
  1. POZend: Two Objects, one Row
    text
    copied!<p>I've recently started using Zend Framework (1.8.4), to provide admin tools for viewing the orders of a shopping cart site.</p> <p>What I'd like to do is to efficiently create multiple model (<code>Zend_Db_Table_Row_Abstract</code>) objects from a single database result row.</p> <p>The relationship is simple: an Order has one Customer (foreign key <code>order_custid=customer.cust_id</code>); a Customer has many Orders.</p> <p>Loading the orders is easy enough. Using the method documented here: </p> <p><a href="https://stackoverflow.com/questions/638622/modeling-objects-with-multiple-table-relationships-in-zend-framework">Modeling objects with multiple table relationships in Zend Framework</a></p> <p>...I could then grab the customer for each.</p> <pre><code> foreach ($orderList as $o) { cust = $o->findParentRow('Customers'); print_r ($cust); // works as expected. } </code></pre> <p>But when you're loading a long list of orders - say, 40 or more, a pageful - this is painfully slow.</p> <p>Next I tried a JOIN:</p> <pre><code> $custTable = new Customers(); $orderTable = new Orders(); $orderQuery = $orderTable->select() ->setIntegrityCheck(false) // allows joins ->from($orderTable) ->join('customers', 'cust_id=order_custid') ->where("order_status=?", 1); //incoming orders only. $orders = $orderTable->fetchAll($orderQuery); </code></pre> <p>This gives me an array of order objects. <code>print_r($orders)</code> shows that each of them contains the column list I expect, in a protected member, with raw field names order_* and cust_*.</p> <p>But how to create a Customer object from the cust_* fields that I find in each of those Order objects?</p> <pre><code> foreach ($orders as $o) { $cols = $o->toArray(); print_r ($cols); // looks good, has cust_* fields... $cust = new Customer(array( 'table' => 'Customer', 'data' => $cols ) ); // fails - $cust->id, $cust->firstname, etc are empty $cust->setFromArray($cols); // complains about unknown 'order_' fields. } </code></pre> <p>Is there any good way to create an Order and a Customer object simultaneously from the joined rows? Or must I run the query without the table gateway, get a raw result set, and copy each of the fields one-by-one into newly created objects?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload