如何使用Symfony2,Doctrine2执行存储过程 [英] How to execute Stored Procedures with Symfony2, Doctrine2

查看:39
本文介绍了如何使用Symfony2,Doctrine2执行存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码:

use Doctrine\ORM\Query\ResultSetMapping;
...
...
...
...
$em    = $this->get( 'doctrine.orm.entity_manager' );
$rsm   = new ResultSetMapping();
$query = $em->createNativeQuery( 'CALL procedureName(:param1, :param2)', $rsm )
            ->setParameters( array(
                'param1' => 'foo',
                'param2' => 'bar'
            ) );
$result = $query->getResult();
//$result = $query->execute(); // Also tried

$em->flush();
die(var_dump($result));

我没有在$ result参数中得到任何东西.谁能告诉我如何从Symfony 2.0.15中的存储过程获取结果?

I am not getting any thing in the $result parameter. Can anyone please tell me how to get the result from a stored procedure in Symfony 2.0.15 ?

推荐答案

我建议使用普通的 PDO .在下面的示例中,我调用一个存储过程并获取 OUT 参数的值.

I would suggest to use plain PDO. In the following example I call a stored procedure and get value of the OUT parameter.

使用 IN OUT 参数的过程:

CREATE PROCEDURE `CLONE_MEMBER_PRODUCT` (IN ID INT, OUT NEW_ID INT)
BEGIN
    /* ... */
END;

getWrappedConnection()返回 Doctrine \ DBAL \ Driver \ Connection 的实例,该实例只是 PDO

getWrappedConnection() returns instance of Doctrine\DBAL\Driver\Connection which is just a wrapper for PDO

/* @var $connection \PDO */
$connection = $this->getEntityManager()
    ->getConnection()
    ->getWrappedConnection();

$stmt = $connection->prepare('CALL CLONE_MEMBER_PRODUCT(?, @NEW_ID)');
$stmt->bindParam(1, $id, \PDO::PARAM_INT);
$stmt->execute();

$stmt = $connection->query("SELECT @NEW_ID");
$id = $stmt->fetchColumn();

这篇关于如何使用Symfony2,Doctrine2执行存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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