问题
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