How to use store with Doctrine

Asked

Viewed 343 times

2

How to use stored Procedure with Doctrine ?

Should I use createNativeQuery ?

2 answers

0

Hello! Thank you for replying Marcelo!

I have 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 a 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 above example, my stored Procedure is in an Oracle package. The :pmsg variable receives the return of my stored past.

0

That I know only using native query, so:

$results = $this->getConnection()->query("SELECT NOME_DA_PROCEDURE()")
    ->fetchAll();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.