How to execute stored procedures using 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 do not get anything in the $ result parameter. Can someone tell me how to get the result from a stored procedure in Symfony 2.0.15?

+5
source share
2 answers

You have not added any result set information. See here for a sample.

+2
source

I would suggest using plain PDO. In the following example, I call the stored procedure and get the value of the parameter OUT.

Procedure with parameters INand OUT:

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

getWrappedConnection()returns an instance Doctrine\DBAL\Driver\Connectionthat is only a wrapper forPDO

/* @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();
+1
source

All Articles