2012-02-23 118 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