如何在Cakephp 3中调用PDOStatement :: nextRowset() [英] How to call PDOStatement::nextRowset() in Cakephp 3

查看:71
本文介绍了如何在Cakephp 3中调用PDOStatement :: nextRowset()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个程序,其中有两个select语句正在运行.

I have created a procedure in which i there are two select statements are running.

$stmt = $this->_connection->execute("CALL myFunction(:user_id)", [
    'user_id' => $userId
]);

但是当我试图像这样调用nextRowset()

But when i am trying to call the nextRowset() like this

$stmt->nextRowset();

它给我错误

调用未定义的方法Cake \ Database \ Log \ LoggingStatement :: nextRowset()

Call to undefined method Cake\Database\Log\LoggingStatement::nextRowset()

所以,我的问题是如何在Cakephp 3中调用nextRowset()

So, My question is how can i call the nextRowset() in Cakephp 3

推荐答案

鉴于所有核心语句类都确实扩展了\Cake\Database\Statement\StatementDecorator,您可以通过StatementDecorator::getInnerStatement()到达基础的本机\PDOStatement对象,例如:

Given that all core statement classes do extend \Cake\Database\Statement\StatementDecorator at some point, you could get to the underlying native \PDOStatement object via StatementDecorator::getInnerStatement(), like:

while ($stmt instanceof StatementDecorator) {
    $stmt = $stmt->getInnerStatement();
}

if (!($stmt instanceof \PDOStatement)) {
    throw new \RuntimeException('Expected an instance of \PDOStatement');
}

然后,您可以使用标准的PDO语句过程,例如在循环中遍历行集:

Then you can use standard PDO statement procedures, like iterating over the rowsets in a loop:

do {
    $rowset = $stmt->fetchAll(PDO::FETCH_ASSOC);
    // ...
} while ($stmt->nextRowset());

正如注释中已经提到的,另一种方式是实现自己的语句类(并使您的代码期望该具体实现的实例).为了实现跨数据库兼容性,您必须实现四个不同的语句,并在其中重新实现\Cake\Database\Driver::prepare()的地方添加四个驱动程序,因为这是生成语句实例的地方.

As already mentioned in the comments, another way would be to implement your own statement class (and make your code expect an instance of that concrete implementation). For cross DB compatibilty you'd have to implement four different statements though, plus four drivers where you'd re-implement \Cake\Database\Driver::prepare(), as this is where the statement instances are being generated.

此外,如果要支持查询日志记录,还必须创建一个自定义连接类并覆盖\Cake\Database\Connection::prepare()\Cake\Database\Connection::_newLogger(),因为这是驱动程序生成的语句包装在\Cake\Database\Log\LoggingStatement中的位置.案例查询日志已启用.

Also in case you want to support query logging, you'd have to create a custom connection class and override \Cake\Database\Connection::prepare() or \Cake\Database\Connection::_newLogger(), as this is where the statements generated by driver are being wrapped in \Cake\Database\Log\LoggingStatement in case query logging is enabled.

我想说的是,如果您要支持的只是内置驱动程序,那么暂时期望\Cake\Database\Statement\StatementDecorator实例可能是更好的选择,即使它并不太好.您可能需要 建议添加功能 来推进多行语句作为增强功能,不确定是否会对此提供支持.

I'd say if all you want to support are the built-in drivers, then expecting \Cake\Database\Statement\StatementDecorator instances is probably the better choice for the time being, even though it's not overly nice. You may want to suggest adding functionality for advancing multi-rowset statements as an enhancement, not sure if there will be much support for it though.

另请参见

这篇关于如何在Cakephp 3中调用PDOStatement :: nextRowset()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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