我创建了一个过程,其中有两个 select 语句正在运行。
$stmt = $this->_connection->execute("CALL myFunction(:user_id)", [
'user_id' => $userId
]);
但是当我尝试像这样调用 nextRowset() 时
$stmt->nextRowset();
它给了我错误
调用未定义的方法 Cake\Database\Log\LoggingStatement::nextRowset()
所以,我的问题是如何在 Cakephp 3 中调用 nextRowset()
鉴于所有核心语句类都扩展了\Cake\Database\Statement\StatementDecorator
在某些时候,您可以访问底层的本机\PDOStatement
对象通过StatementDecorator::getInnerStatement()
, like:
while ($stmt instanceof StatementDecorator) {
$stmt = $stmt->getInnerStatement();
}
if (!($stmt instanceof \PDOStatement)) {
throw new \RuntimeException('Expected an instance of \PDOStatement');
}
然后您可以使用标准 PDO 语句过程,例如在循环中迭代行集:
do {
$rowset = $stmt->fetchAll(PDO::FETCH_ASSOC);
// ...
} while ($stmt->nextRowset());
正如评论中已经提到的,另一种方法是实现您自己的语句类(并使您的代码期望该具体实现的实例)。为了跨数据库兼容性,您必须实现四个不同的语句,再加上四个您需要重新实现的驱动程序\Cake\Database\Driver::prepare()
,因为这是生成语句实例的地方。
另外,如果您想支持查询日志记录,则必须创建自定义连接类并覆盖\Cake\Database\Connection::prepare()
or \Cake\Database\Connection::_newLogger()
,因为这是驱动程序生成的语句被包装在的地方\Cake\Database\Log\LoggingStatement
如果启用了查询日志记录。
我想说,如果您想要支持的只是内置驱动程序,那么期望\Cake\Database\Statement\StatementDecorator
实例可能是目前更好的选择,尽管它不是很好。你可能想要建议添加功能 https://github.com/cakephp/cakephp/issues对于推进多行集语句作为增强功能,不确定是否会有很多支持。
See also
- API > \Cake\Database\Statement\StatementDecorator::getInnerStatement() https://api.cakephp.org/3.3/class-Cake.Database.Statement.StatementDecorator.html#_getInnerStatement
- API > \Cake\Database\Driver::prepare() https://api.cakephp.org/3.3/class-Cake.Database.Driver.html#_prepare
- API > \Cake\Database\Connection::prepare() https://api.cakephp.org/3.3/class-Cake.Database.Connection.html#_prepare
- API > \Cake\Database\Connection::_newLogger() https://api.cakephp.org/3.3/class-Cake.Database.Connection.html#__newLogger
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)