arrayszend-frameworkjoinmultiple-results

Zend join left with multiple result from left table


I am trying to select one item form the items table and join the second table (images). The table images will have multiple results for each item. The problem is that the result join is bringing just one result instead of an array with all images data.

The code

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

What I am missing?

Thanks


Solution

  • in your post you have $result = $this->fetchRoll($select); i think its a typo error you might be doing $result = $this->fetchRow($select); in your code

    but you should use fetchAll instead:

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

    see here http://framework.zend.com/manual/en/zend.db.table.html

    EDIT : to get the item's data array with a sub array with all images

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