本文介绍了此结果是仅向前结果集,不支持向前移动后调用rewind()-Zend的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Zend应用程序中,我使用Zend\Db\TableGatewayZend\Db\Sql如下所示从MySQL数据库检索数据数据.

In Zend app, I use Zend\Db\TableGateway and Zend\Db\Sql to retrieve data data from MySQL database as below.

模型-

public function getCandidateEduQualifications($id)
{
    $id  = (int) $id;

    $rowset = $this->tableGateway->select(function (Sql\Select $select) use ($id)
    {
        $select->where
            ->AND->NEST->equalTo('candidate_id', $id)
            ->AND->equalTo('qualification_category', 'Educational');
    });

    return $rowset;
}

查看-

我只是迭代$ rowset并在视图中回显.但是,当尝试回声两次或更多次时,它会产生错误.单次迭代有效.

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

我可以通过将其加载到视图中的另一个数组来解决它.但这是最好的方法吗?还有其他方法可以解决这个问题吗?

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 来获取其Zend\Db\ResultSet\Resultset,该Zend\Db\ResultSet\Resultset由. PDO结果集默认情况下使用只进游标,这意味着您只能在该结果集中循环一次.

You receive this Exception because this is expected behavior. Zend uses PDO to obtain its Zend\Db\ResultSet\Resultset which is returned by Zend\Db\TableGateway\TableGateway. 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.

随着Zend\Db\ResultSet\Resultset实现PHP Iterator ,您可以提取使用Zend\Db\ResultSet\Resultset:toArray()方法或iterator_to_array()函数的集合的数组.不过,在可能较大的数据集上使用此功能时请务必小心!关于游标的最好的事情之一就是,如果数据集太大的话,它们避免一次性输入所有内容,因此有时您不想一次将它们全部放入一个数组中.

As the Zend\Db\ResultSet\Resultset implements the PHP Iterator you can extract an array of the set using the Zend\Db\ResultSet\Resultset: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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 15:40