問題描述
在 Zend 應用程序中,我使用 ZendDbTableGateway
和 ZendDbSql
從 MySQL 數據庫中檢索數據數據,如下所示.
In Zend app, I use ZendDbTableGateway
and ZendDbSql
to retrieve data data from MySQL database as below.
模型 -
public function getCandidateEduQualifications($id)
{
$id = (int) $id;
$rowset = $this->tableGateway->select(function (SqlSelect $select) use ($id)
{
$select->where
->AND->NEST->equalTo('candidate_id', $id)
->AND->equalTo('qualification_category', 'Educational');
});
return $rowset;
}
查看 -
我只是在視圖中迭代 $rowset 和 echo.但是當嘗試回顯兩次或更多次時它會出錯.單次迭代有效.
I just iterate $rowset and echo in view. But it gives error when try to echo two or more times. Single iteration works.
這個結果是一個只向前的結果集,在調用rewind()之后不支持前進
This result is a forward only result set, calling rewind() after moving forward is not supported
我可以通過將其加載到視圖中的另一個數組來解決它.但這是最好的方法嗎?有沒有其他辦法可以解決這個問題?
I can solve it by loading it to another array in view. But is it the best way ? Is there any other way to handle this ?
$records = array();
foreach ($edu_qualifications as $result) {
$records[] = $result;
}
編輯 -
$resultSet->buffer();
解決了這個問題.
推薦答案
您收到此 Exception
因為這是預期的行為.Zend 使用 PDO 來獲取它的 ZendDbResultSet
.PDO 結果集默認使用只進游標,這意味著您只能循環一次.ZendDbTableGatewayTableGateway
返回的結果集
You receive this Exception
because this is expected behavior. Zend uses PDO to obtain its ZendDbResultSetResultset
which is returned by ZendDbTableGatewayTableGateway
. PDO result sets use a forward-only cursor by default, meaning you can only loop through the set once.
有關游標的更多信息,請查看維基百科和這個 文章.
For more information about cursors check Wikipedia and this article.
作為 ZendDbResultSetResultset
實現 PHP Iterator
您可以使用 ZendDbResultSetResultset:toArray()
方法或使用 iterator_to_array()
函數.在潛在的大型數據集上使用此函數時請務必小心!關于游標的最好的事情之一正是它們避免一次性引入所有內容,以防數據集太大,因此有時您不想一次將其全部放入數組中.
As the ZendDbResultSetResultset
implements the PHP Iterator
you can extract an array of the set using the ZendDbResultSetResultset:toArray()
method or using the iterator_to_array()
function. Do be careful though about using this function on potentially large datasets! One of the best things about cursors is precisely that they avoid bringing in everything in one go, in case the data set is too large, so there are times when you won't want to put it all into an array at once.
這篇關于此結果是僅向前的結果集,不支持向前移動后調用 rewind() - Zend的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!