14

I am trying to create stored procedure in Cakephp. I have created Procedure in MYSQL and created global function in AppModel.php to hit procedure. The function in AppModel is sProcedure. Now I have 2 conditions I might have variable to return from procedure or may have direct resultset eg. I have created for Pagination. Though its shooting my procedure but not return any result. Is my function require modification?

    public function sProcedure($name = NULL, $inputParameter = array(), $outputParameter = array()) {
        $this->begin();
        $parameter = "";
        $outputParam = "";
        foreach ($inputParameter as $params) {
            $parameter .= $parameter == "" ? " '$params' " : ", '$params' ";
        }

        if (count($outputParameter) > 0) {
            foreach ($outputParameter as $prm) {
                $outputParam .= $outputParam == "" ? " @$prm " : ", @$prm ";
            }
        }
        $parameter = ($outputParam) ? $parameter . ", " . $outputParam : $parameter;
        $this->query("CALL `$name`($parameter);");
        $result = $this->query("SELECT $outputParam");
        $this->commit();
        return $result;
    }

    $sel_data = $this->ArticleNews->sProcedure("update_blank", $input_prameter, $output);
    debug($sel_data);
4
  • are you getting any error
    – Aman Rawat
    Commented May 18, 2015 at 10:15
  • I am not getting error, My procedure executes as well. But I wish to know how to take output from procedure. Using same function. No matter if I have output resultset or variables.
    – Sankalp
    Commented May 18, 2015 at 23:11
  • @Sankalp is the procedure returning legit values when called directly from MySQL (not using PHP)? Also, $this->query executes the query string passed and returns (if not failing) a resource that should be iterated over.
    – sitilge
    Commented May 19, 2015 at 10:55
  • Yes its returning correct values. Issues is sometimes I return dataset from SQL like in Pagnation. in that case Do I need to mention column name or simply SELECT * in procedure. How to read response in that case. In that scenerio I won't mention output variables. But If I need specific column like count or any other function I would mention output varibles. Would it work with both. Is it require moderation.
    – Sankalp
    Commented May 20, 2015 at 6:45

3 Answers 3

13
+50

Breaking down the snippet

$this->query("CALL `$name`($parameter);");

calls stored procedure. Lets assume that the procedure's body is a simple SELECT

SELECT table.* FROM table;

This procedure returns a result set. Take a quick look at the code you provided

 $this->query("CALL `$name`($parameter);");

Yes, the procedure is called but the result/resource is not assigned to a variable to iterate over.

$proc_result = $this->query("CALL `$name`($parameter);");

$proc_data = array();

if (false !== $proc_result)
{
    while ($proc_row = mysqli_fetch_array($proc_result))
    {
        $proc_data[] = $proc_row; 
    }
}

if (!empty($proc_data))
{
    //do whatever with procedure data
}

Related documentation: connector-cpp-tutorials-stored-routines-statements

3
  • Hi you have taken the first scenario, Case when I don't have output variables, means I have simple SELECT recordset Query. My doubt with this, Is mysqli_fetch_array follows standard of CakePHP framework that I am working, I wish to follow standard of Framework as well. Case II: I have selected vars for the result. I would put query this way. $proc_result = $this->query("CALL $name($parameter);"); if($outputParam) { $result = $this->query("SELECT $outputParam"); } else { while ($proc_row = mysqli_fetch_array($proc_result)) { $proc_data[] = $proc_row; } } Correct ??
    – Sankalp
    Commented May 25, 2015 at 6:26
  • Well, CakePHP is a framework - something that is built upon an existing piece of code. It does not mean that it will cover up all aspects of the fundamental code that is PHP. What I want to say is - you will have to write something in 'pure' PHP as the framework might not provide such functionality. TLDR; here is a nice artice from CakePHP docs: book.cakephp.org/2.0/en/models/retrieving-your-data.html. However, it is totally correct to use non-framework methods. :)
    – sitilge
    Commented May 25, 2015 at 7:40
  • Please check my code I have attached for final test case.
    – Sankalp
    Commented May 25, 2015 at 14:10
4

Use following code to call stored procedure cakephp 3

Its working for me.....

$this->connection = ConnectionManager::get('default');
$meritlists = $this->connection->execute("CALL generateMeritList()")->fetchAll('assoc');
0

Guys DONT bash your heads

check those couple of lines:

$sql="CALL `delete_category`(".$id.");";
$db3= ConnectionManager::getDataSource('default');
$db3->query($sql);

guaranteed to work. the getDatasource returns an $mysqli object so u can use it

check the PHP manual for sp Calling a stored procedure in php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.