如何在 ZF2 zend-db 中缓冲未缓冲的 ResultSet? [英] How can I buffer an unbuffered ResultSet in ZF2 zend-db?

查看:33
本文介绍了如何在 ZF2 zend-db 中缓冲未缓冲的 ResultSet?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Atlas 类执行递归数据探索,给定一组不连续但连续的 col1 值,从 MySQL table1 开始读取$startingVal.限制 $totalVal 是任何给定会话期间工作单元的限制.在每次递归时,Atlas 将从不同的表中检索零到八 (0-8) 行.

My Atlas class performs a recursive data exploration given a non-contiguous but sequential set of col1 values, read from MySQL table1 beginning with $startingVal. Limit $totalVal is a throttle for work units during any given session. On each recursion, Atlas will retrieve between zero and eight (0-8) rows from a different table.

致命错误:未捕获的异常 'Zend\Db\Adapter\Exception\RuntimeException'
带有消息行计数在无缓冲的结果集中不可用."
在/path/vendor/Zend/zend-db/src/Adapter/Driver/Mysqli/Result.php:316
堆栈跟踪:
#0/path/vendor/Zend/zend-db/src/ResultSet/AbstractResultSet.php(65):
Zend\Db\Adapter\Driver\Mysqli\Result->count()
#1/path/vendor/Zend/zend-db/src/Adapter/Adapter.php(195):
Zend\Db\ResultSet\AbstractResultSet->
初始化(对象(Zend\Db\Adapter\Driver\Mysqli\Result))
#2/path/modules/Flightplan/src/Flightplan/Atlas.php(110):Zend\Db\Adapter\Adapter->query('SELECT col1...', Array)
#3/path/utility.php(46):
Flightplan\Atlas->shoulder_routes('30000001', '1')
#4 {main} 在/path/vendor/Zend/zend-db/src/Adapter/Driver/Mysqli/Result.php
在线 316

Fatal error: Uncaught exception 'Zend\Db\Adapter\Exception\RuntimeException'
with message 'Row count is not available in unbuffered result sets.'
in /path/vendor/Zend/zend-db/src/Adapter/Driver/Mysqli/Result.php:316
Stack trace:
#0 /path/vendor/Zend/zend-db/src/ResultSet/AbstractResultSet.php(65):
Zend\Db\Adapter\Driver\Mysqli\Result->count()
#1 /path/vendor/Zend/zend-db/src/Adapter/Adapter.php(195):
Zend\Db\ResultSet\AbstractResultSet->
initialize(Object(Zend\Db\Adapter\Driver\Mysqli\Result))
#2 /path/modules/Flightplan/src/Flightplan/Atlas.php(110): Zend\Db\Adapter\Adapter->query('SELECT col1...', Array)
#3 /path/utility.php(46):
Flightplan\Atlas->shoulder_routes('30000001', '1')
#4 {main} thrown in /path/vendor/Zend/zend-db/src/Adapter/Driver/Mysqli/Result.php
on line 316

如果我正确读取错误和 Zend 源代码,Adapter 正在使用 Mysqli\Result 初始化一个 AbstractResultSet(默认缓冲)资源(默认情况下不缓冲),然后立即尝试对其进行计数,这会引发错误.这发生在我第一次通过 Adapter 查询期间,在递归之前.

If I am reading the error and Zend source correctly, Adapter is initializing an AbstractResultSet (buffering by default) with a Mysqli\Result resource (non buffering by default), and immediately after, tries to count it, which throws the error. This is happening the during my first query through Adapter, before recursion.

public function initialize($dataSource)
{
    // reset buffering
    if (is_array($this->buffer)) {
        $this->buffer = array();
    }

    if ($dataSource instanceof ResultInterface) {
        $this->count = $dataSource->count();         // line 65

Atlas.php:

use Zend\Db\Adapter\Adapter;

public function __construct( array $configArray = null ){
    ...
    $this->configArray = $configArray;
    $this->adapter = new Adapter( $this->configArray );
}
public function shoulder_routes( $startingVal, $totalVal ){
    ...
    $result = $this->adapter->query( 'SELECT col1 FROM table1 
        WHERE col1 >= ? LIMIT ?', array( $startingVal, $totalVal ));  // line 110
    $result->buffer(); // ADDED AFTER 1st error occurrence 
    ...
}

我添加了 $result->buffer(); 不幸的是没用,因为错误发生在执行返回到 Atlas 之前.

I added $result->buffer(); which is unfortunately useless since the error occurs before execution returns to Atlas.

  1. Mysqli\Result 可以在初始化时设置为缓冲区.我是否在默认情况下使用缓冲来扩展 Result,我是否会在 Atlas 中指定它,例如:
  1. Mysqli\Result CAN be set to buffer at initialization. Do I extend Result with buffering on by default, and would I specify that in Atlas, like:

$this->adapter = new Adapter( $this->configArray,null,new MysqliResult());

将 ResultInterface 作为参数提供给 Adapter 会引发错误,因为 Adapter 需要一个 ResultSet.但是,可以扩展 AbstractResultSet 以便 IT 在对从 Adaper\Driver\Mysqli 传递给它的 ResultSet 调用 $result->count() 之前调用 $result->buffer()...>

  1. (我认为)我读到 Zend\Adapter PDO_Mysql driver/platform/result 默认情况下会缓冲.有什么理由不改用 PDO?

切换到 PDO_Mysql Adapter\Driver 导致以下错误,我想如果我准备驱动程序提供的语句可能会解决这个问题

未捕获的异常 'PDOException' 带有消息 'SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以获取在/path/vendor/Zend/zend-db/src/Adapter/Driver/Pdo/Statement.php 中第 1 行的1"附近使用的正确语法:239

Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''1'' at line 1' in /path/vendor/Zend/zend-db/src/Adapter/Driver/Pdo/Statement.php:239

  1. 我也在这个答案中看到过 where result->getResource()->fetchAll(); 稍后可能会在 Atlas 中有所帮助,但在我得到这个机会之前再次发生错误.立>
  1. I have also seen in this answer where result->getResource()->fetchAll(); may help later in Atlas but again the error is occurring before I get this chance.

我错过了什么?谢谢-

推荐答案

从这个问题和项目来来去去几个月后,我偶然发现了一个答案此处这让我得到了相同的答案 此处.

After coming and going from this problem and project for a few months, I stumbled upon an answer here which lead me to the same answer here.

    $config => array(
       ...
      'options' => array(
        'buffer_results' => true,
    ),

我一直在寻找这种答案,但显然是盯着看太用力了.

I was looking for this kind of answer, and obviously stared too hard.

这篇关于如何在 ZF2 zend-db 中缓冲未缓冲的 ResultSet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆