How to use store procedure with Doctrine

1

How do I use stored procedure with Doctrine?

Should I use createNativeQuery?

    
asked by anonymous 03.05.2016 / 15:36

2 answers

0

I only know using native query, like this:

$results = $this->getConnection()->query("SELECT NOME_DA_PROCEDURE()")
    ->fetchAll();
    
03.05.2016 / 23:44
0

Hello! Thanks for replying Marcelo!

I found the means to do it. There are two methods:

1) Using createNativeQuery

    $rsm = new ResultSetMapping;
    $em = $this->getEntityManager();
    $sp = "BEGIN pk_web.sp_WEB_CONTATO(:pNOME,:pEMAIL,:pFONE,:pMENSAGEM); END;";
    $query = $em->createNativeQuery($sp,$rsm)
        ->setParameters(array(
            ':pNOME'            => $data['nome'],
            ':pEMAIL'           => $data['email'],
            ':pFONE'            => $data['fone'],
            ':pMENSAGEM'        => $data['mensagem'],
        ));
    $result = $query->getResult();

2) Using PDO

$connection = $this->getEntityManager()
        ->getConnection()
        ->getWrappedConnection();

    $stmt = $connection->prepare("BEGIN pk_web.sp_WEB_CONTATO(:pNOME,:pEMAIL,:pFONE,:pMENSAGEM,:pMSG); END;");

    $stmt->bindParam(":pNOME",           $data['nome'],      \PDO::PARAM_STR, 255);
    $stmt->bindParam(":pEMAIL",          $data['email'],     \PDO::PARAM_STR, 255);
    $stmt->bindParam(":pFONE",           $data['fone'],      \PDO::PARAM_STR, 255);
    $stmt->bindParam(":pMENSAGEM",       $data['mensagem'],  \PDO::PARAM_STR, 8000);

    $stmt->bindParam(":pMSG",$msg,\PDO::PARAM_STR, 1000);

    $stmt->execute();

    if($msg) {
        return [
            "success" => true,
            "id" => $msg,
            "nome" => $data['nome'],
            "email" => $data['email']
        ];
    }

In the example above, my stored procedure is in an Oracle package. The variable: pmsg receives the return from my stored procedure.

    
05.05.2016 / 19:29