Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Stored procedure in Phalcon

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



18.1k
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 = strreplace('"',"'",$value); $data[$key] = '"'.$value.'"'; } $query = $db->query( 'CALL '.$name.'('.implode(',',$data).')' ); $query->setFetchMode(\Phalcon\Db::FETCHASSOC); 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