编程语言
首页 > 编程语言> > php-在Zend模型中使用多个表并返回组合的结果集

php-在Zend模型中使用多个表并返回组合的结果集

作者:互联网

嗨,这是一个非常具体或非常通用的问题-我不确定,并且我对Zend框架/ oo来说是新手.如果这是一个愚蠢的问题,请耐心等待…

无论如何,我想创建一个执行以下操作的模型:

Read all the itmes from a table 'gifts' into a row set

for each row in the table, read from a second table which shows how many have been bought, the append this as another "field" in the returned row

return the row set, with the number bought included.

大多数简单的Zend示例似乎只在模型中使用一个表,但是我的阅读似乎表明我应该在此处执行大部分工作,而不是在控制器中.如果这是一个太笼统的问题,那么任何可以处理2个表并返回数组的模型示例都很棒!

谢谢您的帮助!

解决方法:

我假设第二张表是“ 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;

如果这是您要找的东西,可以在此链接上找到更多内容link 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),循环播放或编辑

}

标签:zend-framework,php
来源: https://codeday.me/bug/20191210/2103746.html