Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
352 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 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...