2013-03-18 65 views
0

如何通過使用foreach循環來循環控制我的db結果到控制器中?訪問控制器中的zf2數據庫結果對象

$select = new Select(); 
$select->from('table_name'); 
$select->where(array('salt' => $salt)); 
$select->where(array('ip' => $this->_getUserIp())); 

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

return $rowset; 

我想我需要將數據庫結果對象轉換爲數組?

在此先感謝

回答

0

http://framework.zend.com/manual/2.1/en/modules/zend.db.result-set.html

Zend\Db\ResultSet\ResultSet延伸traversable,因此你可以使用它foreach

foreach($this->getSomeTable()->fetchAll() as $row) { 
    //here you can access the row as an array or use getters if you have set a prototype object 
    //eg 
    $userId = $row['user_id']; 
    $userId = $row->user_id; 
    $userId = $row->getId(); 

} 

此外,我建議您閱讀入門指南。所有這些基本的東西在那裏解釋。

http://framework.zend.com/manual/2.1/en/user-guide/overview.html

+0

感謝您的回答,我現在移除了module.php的「setArrayObjectPrototype」,這並獲得成功對我來說:) – directory 2013-03-19 08:08:29