CakePHP与存储过程 [英] CakePHP with Stored Procedure

查看:181
本文介绍了CakePHP与存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Cakephp中创建存储过程。我在MYSQL中创建了过程,并在AppModel.php中创建了全局函数来命中过程。 AppModel中的函数是sProcedure。现在我有2个条件我可能有变量从过程返回或可能有直接结果集例如。我为分页创建。虽然它拍摄我的程序,但不返回任何结果。

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);


推荐答案

分解片段

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

调用存储过程。让我们假设过程的主体是一个简单的 SELECT

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);");

,将调用此过程,但结果/ 资源未分配给变量iterate

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
}

相关文档:connector-cpp-tutorials-stored-routines-statements

这篇关于CakePHP与存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆