我假设第二张桌子类似于“gift_order”之类的东西。
在这种情况下,您需要通过外键指定“gift”和“gift_order”之间的表关系,并在表类中描述它。
It will look like this
class GiftOrder extends Zend_Db_Table_Abstract
{
/** Table name */
protected $_name = 'gif_order';
protected $_referenceMap = array(
"Fileset" =>array(
"columns" => array("gifId"),
"refTableClass" => "Gift",
"refColumns" => array("id")
));
........................
You need to specify foreigh key constraint while create table with SQL
ALTER TABLE `gift_order`
ADD CONSTRAINT `order_to_gift` FOREIGN KEY (`giftId`) REFERENCES `gift` (`id`) ON DELETE CASCADE;
如果这是您正在寻找的东西,您可以在此链接链接 http://framework.zend.com/manual/en/zend.db.table.relationships.html中找到更多信息
有了这样的解决方案,您将能够循环礼物并获得他们的订单,而无需任何复杂的 SQL
$rowSetGifts = $this->findGifts();
while($rowSetGifts->next()){
$gift = $rowSetGifts->current();
$orders = $gift->findGiftOrder();//This is magick methods, this is the same $gift->findDependentRowset('GiftOrder');
//现在你可以对订单做一些事情 - count($orders),循环它们或编辑
}