2013-04-11 71 views
1

我想從我的數據庫中獲得一些行,其中一個字段有一個特定的值。 我試過這段代碼:Zend Framework 2得到多個行

public function getJewelrybyCollection($collection) 
{ 
    $rowset = $this->tableGateway->select(array('collection' => $collection)); 
    $row = $rowset->current(); 
    if (!$row) { 
     throw new \Exception("Could not find collection $collection"); 
    } 
    return $row; 
} 

這是工作,但僅檢索一行,由於$row = $rowset->current(); 我試圖返回$rowset變種,沒有好的結果。

我是新來ZF2

謝謝

+1

嘗試在RowSet – Abadis 2013-04-11 06:43:16

回答

3

你需要了解你實際上在做什麼。請回頭參考official manual並再次嘗試。

$rowset = $this->tableGateway->select(...); 

$rowset將是一個Zend\Db\ResultSet\ResultSet。這ResultSet包含您指定查詢的所有Rows

$row = $rowset->current() 

這一行你得到你的ResultSet的第一個(最新)。而不是檢查if (!$row)您可以切換到if(0 === $rowset->count()),然後您只能返回$rowset。作爲@Abadis指出,在ResultSet你只需把foreach -loop來訪問每一個你Rows

+0

古怪的一個foreach,我這個昨晚試過了,沒有結果。試了一遍,現在它的工作。我認爲昨天晚上有點晚了!謝謝!! – Royw 2013-04-11 08:11:32