Rashy
Rashy

Reputation: 911

How to execute Stored Procedures with Symfony2, Doctrine2

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 ?

Upvotes: 5

Views: 9899

Answers (2)

b.b3rn4rd
b.b3rn4rd

Reputation: 8840

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

Upvotes: 2

Mun Mun Das
Mun Mun Das

Reputation: 15002

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

Upvotes: 2

Related Questions