2012-02-23 124 views
0

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

代码

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

我缺少什么?

感谢

在您的文章
+0

什么是fetchRoll?它看起来像fetchAll,但它也可以是fetchRow .. :) – Liyali 2012-02-23 13:53:51

+0

结果将始终是单个项目,此数组将具有与此项目相关的每个图像(连接结果)的数据的另一个子数组。 – Bema 2012-02-23 14:10:41

回答

0

你有$result = $this->fetchRoll($select); 我想你可能在你的代码

做了一个错字错误 $result = $this->fetchRow($select);,但你应该使用使用fetchall:

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

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

编辑:获取项目的数据阵列与所有图像的子阵列

$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; 
+0

感谢您的帮助MMC 使用$ this-> fetchAll($ select)我得到一个重复多次的相同项目的数组,因此如果这个项目有四个图像,结果数组将是这个项目的四倍,每个项目有一个不同的图像数据。 我想要结果是与所有图像的子数组的项目的数据数组。 试图使用$ select-> group('items.item_id'),但它只带来一个图像。 – Bema 2012-02-23 14:05:16

+0

我添加了一个编辑来获取项目的数据数组与图像的子阵列 – 2012-02-23 14:21:00

+0

我看到你的解决方案MMC,我实际上认为我可以带来的结果很好直接从数据库,而不是用循环调整,但这效果很好,不会消耗mucho处理。 非常感谢MMC! – Bema 2012-02-23 14:30:45