Symfony2原则原始SQL存储过程通过多个结果集循环
我已设法执行原始SQL(即无结果映射),并可以调用和执行MSSQL存储过程 我的守则如下:Symfony2原则原始SQL存储过程通过多个结果集循环,symfony,stored-procedures,doctrine-orm,Symfony,Stored Procedures,Doctrine Orm,我已设法执行原始SQL(即无结果映射),并可以调用和执行MSSQL存储过程 我的守则如下: $em = $this->get('doctrine')->getManager(); $stmt = $em ->getConnection() ->prepare('EXEC someSP :id,null,:uid'); $stmt->bindValue('id', '629674'); $stmt->bindValue('uid', '217')
$em = $this->get('doctrine')->getManager();
$stmt = $em
->getConnection()
->prepare('EXEC someSP :id,null,:uid');
$stmt->bindValue('id', '629674');
$stmt->bindValue('uid', '217');
$stmt->execute();
$results = $stmt->fetchAll();
不,那很好;但是,我遇到的问题是,如果SP返回多个结果集,则上述结果集只返回第一个结果集。有没有办法循环并获得每个结果集?您可以尝试逐个对每个结果集进行评分。但真的,所有人都应该像下面一样去做,尽管试一下也无妨
$em = $this->get('doctrine')->getManager();
$stmt = $em
->getConnection()
->prepare('EXEC someSP :id,null,:uid');
$stmt->bindValue('id', '629674');
$stmt->bindValue('uid', '217');
$stmt->execute();
do{
$results[] = $stmt->fetchAll()
} while($stmt->nextRowset());
你可以试着一个接一个地对每一组进行评分。但真的,所有人都应该像下面一样去做,尽管试一下也无妨
$em = $this->get('doctrine')->getManager();
$stmt = $em
->getConnection()
->prepare('EXEC someSP :id,null,:uid');
$stmt->bindValue('id', '629674');
$stmt->bindValue('uid', '217');
$stmt->execute();
do{
$results[] = $stmt->fetchAll()
} while($stmt->nextRowset());
好的,我能够做到这一点,但我必须围绕连接创建自己的包装: 这充其量只是一种变通方法,但代码基本上使用conf.yml中的DBAL参数创建了一个新的PDO连接。它准备并执行语句,并返回所有结果集 代码可在此处免费使用:
好的,我可以做到这一点,但我必须围绕连接创建自己的包装: 这充其量只是一种变通方法,但代码基本上使用conf.yml中的DBAL参数创建了一个新的PDO连接。它准备并执行语句,并返回所有结果集 代码可在此处免费使用:
我不得不处理类似的问题,这就是我想到的。此函数来自扩展Doctrine\ORM\EntityRepository的类,因此我可以访问_em属性。在一般情况下,您可以使用
$This->get('Doctrine')->getManager()代码>以获取实体管理器
protected function execMultiSetQuery($query, $params, $connection = 'default') {
// Init
$conn = $this->_em
->getConnection($connection)
->getWrappedConnection();
// Processing
if ($conn instanceof \Doctrine\DBAL\Driver\PDOConnection) {
$stmt = $conn->prepare($query);
$stmt->execute($params);
// Loop through the row sets
$results = array();
do {
try {
$results[] = $stmt->fetch(\PDO::FETCH_ASSOC);
}
catch (\Exception $e) {}
} while($stmt->nextRowset());
$stmt->closeCursor(); // Clean up
return $results;
}
else {
return false;
}
}
我不得不处理类似的问题,这就是我想到的。此函数来自扩展Doctrine\ORM\EntityRepository的类,因此我可以访问_em属性。在一般情况下,您可以使用$This->get('Doctrine')->getManager()代码>以获取实体管理器
protected function execMultiSetQuery($query, $params, $connection = 'default') {
// Init
$conn = $this->_em
->getConnection($connection)
->getWrappedConnection();
// Processing
if ($conn instanceof \Doctrine\DBAL\Driver\PDOConnection) {
$stmt = $conn->prepare($query);
$stmt->execute($params);
// Loop through the row sets
$results = array();
do {
try {
$results[] = $stmt->fetch(\PDO::FETCH_ASSOC);
}
catch (\Exception $e) {}
} while($stmt->nextRowset());
$stmt->closeCursor(); // Clean up
return $results;
}
else {
return false;
}
}
谢谢你的回复,我试过了,但没有成功:(开始认为这可能是MS的pdo_sqlsrv驱动程序的工作方式..为了移动到下一个结果集,您必须执行sqlsrv_next_result
,但不确定如何在symfony2/doctrine2中执行此操作。您不能仅从代码内部执行此操作,类似于我使用wrapp中的代码将答案更改为上面的内容呃?不幸的是,nextRowset()在条令中不存在。或者至少我无法让它工作。谢谢你的回答,我试过了,但没有成功:(开始认为这可能是MS的pdo_sqlsrv驱动程序的工作方式..为了移动到下一个结果集,您必须执行sqlsrv_next_result
,但不确定如何在symfony2/doctrine2中执行此操作。您不能仅从代码内部执行此操作,类似于我使用wrapp中的代码将答案更改为上面的内容呃?不幸的是,nextRowset()在条令中不存在……或者至少我无法让它工作。。