Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
456 views
in Technique[技术] by (71.8m points)

php - This result is a forward only result set, calling rewind() after moving forward is not supported - Zend

In Zend app, I use ZendDbTableGateway and ZendDbSql to retrieve data data from MySQL database as below.

Model -

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

View -

I just iterate $rowset and echo in view. But it gives error when try to echo two or more times. Single iteration works.

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

EDIT -

$resultSet->buffer(); solved the problem.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...