【问题标题】:Zend join left with multiple result from left tableZend 左连接左表的多个结果
【发布时间】:2012-03-13 22:23:50
【问题描述】:

我正在尝试从项目表中选择一个项目并加入第二个表(图像)。表格图像将为每个项目提供多个结果。问题是结果连接只带来一个结果,而不是包含所有图像数据的数组。

代码

    $select = $this->select();
    $select->setIntegrityCheck(false);
    $select->from($this)
           ->joinLeft('items_images', 'items.item_id = image_item_id')
           ->where($where);
    $result =  $this->fetchRoll($select);

我错过了什么?

谢谢

【问题讨论】:

  • 什么是 fetchRoll?它看起来像 fetchAll 但它也可能是 fetchRow.. :)
  • 结果将始终是单个项目,该数组将有另一个子数组,其中包含与该项目关联的每个图像的数据(连接的结果)。

标签: arrays zend-framework join multiple-results


【解决方案1】:

在您的帖子中,您有$result = $this->fetchRoll($select); 我认为您可能正在做错字错误 $result = $this->fetchRow($select); 在您的代码中

但你应该改用 fetchAll:

$result =  $this->fetchAll($select);

看这里http://framework.zend.com/manual/en/zend.db.table.html

EDIT:获取项目的数据数组,其中包含所有图像的子数组

$results =  $this->fetchAll($select);

$item['item_id'] = $result[0]['item_id'];
//put other item's data here in item
$images = array();
$i = 0;
foreach($results as $result){
  $images[$i]['image_id'] = $result['image_id']
  $images[$i]['image_name'] = $result['image_name']
  //put other image's data here in $images[$i]
  $i++;
}

$item['images'] = $images;

【讨论】:

  • 感谢 MMC 的帮助 使用 $this->fetchAll($select) 我得到一个数组,其中同一个项目重复了很多次,所以如果这个项目有四个图像,结果数组将是这个的四倍item,每个item都有不同的图像数据。我希望结果是项目的数据数组,其中包含一个包含所有图像的子数组。尝试使用 $select->group('items.item_id') 但它只带来一张图片。
  • 我添加了一个编辑来获取包含图像子数组的项目数据数组
  • 我看到了您的解决方案 MMC,我实际上认为我可以直接从数据库中获取格式正确的结果,而不是通过循环对其进行调整,但这很好用并且不会消耗太多处理。非常感谢MMC!!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-08
  • 1970-01-01
  • 1970-01-01
  • 2018-02-11
相关资源
最近更新 更多