How to execute Stored Procedures with Symfony2, Doctrine2

♀尐吖头ヾ 提交于 2021-02-07 07:32:14

问题


I am using the following code:

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));

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 ?


回答1:


You haven't added any resultset mapping info. See here for sample.




回答2:


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

Procedure with IN and OUT parameters:

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

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();


来源:https://stackoverflow.com/questions/12512868/how-to-execute-stored-procedures-with-symfony2-doctrine2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!