We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Stored procedure in Phalcon

Help! How to execute a stored procedure in Phalcon? thanks.



33.8k
Accepted
answer
edited Aug '14
$sql = "CALL storedProcedure();";
$myModel = new MyModel();
$result = $myModel->getReadConnection()->query($sql)->fetch(); // Or fecthAll()
edited Oct '15

it's working for me, But data is reponse repeating data as below.

[{"id":"248","0":"248","name":"Sameer","1":"Sameer","email":"[email protected]","2":"[email protected]"}]

  • id: 248
  • 0:248
  • name: sameer
  • 1: sameer
edited Oct '15

You can create a function to call stored procedure in your model Here is an example: /*

  • Call Stored Procedure
  • @author: SirTam
  • @email: [email protected]
  • @param: name
  • @param: data
  • @return: TRUE/FALSE */ public function callStored($name='',$data=[],$mode='all'){ $db = $this->getDI()->get('db'); foreach($data as $key=>$value){ $value = str_replace('"',"'",$value); $data[$key] = '"'.$value.'"'; } $query = $db->query( 'CALL '.$name.'('.implode(',',$data).')' ); $query->setFetchMode(\Phalcon\Db::FETCH_ASSOC); if($mode=='one'){ return $query->fetch(); } return $query->fetchAll(); }
edited Oct '15

Tran.. thanks i did it another way. Created method in Model and called the same from controller Model:

          public static function callMyPro($params)
          {
              // A raw SQL statement
              $sql = "CALL my_procedure($params);";

              // Base model
              $robot = new MyModel();

              // Execute the query
              return new Resultset(null, $robot, $robot->getReadConnection()->query($sql));
          }

Controller: $myModel = new MYModel(); $results = $myModel->callMyPro($params)->toArray();

And it works for me.

Thanks