带存储过程的 CakePHP

CakePHP with Stored Procedure

我正在尝试在 Cakephp 中创建存储过程。我在 MYSQL 中创建了过程并在 AppModel.php 中创建了全局函数来命中过程。 AppModel 中的函数是sProcedure。现在我有 2 个条件,我可能有变量 return 从程序或可能有直接结果集,例如。我为分页创建了。虽然它拍摄了我的程序但没有 return 任何结果。我的功能需要修改吗?

    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

SELECT table.* FROM table;

本程序returns一个结果集。快速浏览一下您提供的代码

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

,调用了程序,结果/resource is not assigned to a variable to iterate结束了。

$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 3

它对我有用......

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

伙计们不要bash你的头

检查那几行:

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

保证工作。 getDatasource returns 一个 $mysqli 对象,所以你可以使用它

查看 PHP sp 手册 Calling a stored procedure in php