【问题标题】:CakePHP with Stored ProcedureCakePHP 与存储过程
【发布时间】:2015-05-15 08:23:08
【问题描述】:

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

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

【问题讨论】:

  • 您是否遇到任何错误
  • 我没有收到错误,我的程序也执行了。但我想知道如何从程序中获取输出。使用相同的功能。无论我是否有输出结果集或变量。
  • @Sankalp 是直接从 MySQL(不使用 PHP)调用时返回合法值的过程吗?此外,$this->query 执行传递的查询字符串并返回(如果没有失败)应该迭代的资源。
  • 是的,它返回正确的值。问题是有时我从 SQL 中返回数据集,就像在 Pagnation 中一样。在这种情况下,我是否需要在程序中提及列名或只是 SELECT *。在这种情况下如何阅读响应。在那个场景中,我不会提到输出变量。但是如果我需要特定的列,比如 count 或任何其他函数,我会提到输出变量。两者都可以吗。是否需要适度。

标签: php mysql cakephp stored-procedures


【解决方案1】:

分解sn-p

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

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

SELECT table.* FROM table;

此过程返回一个结果集。快速查看您提供的代码

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

是的,过程被调用但结果/resource没有赋值给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

【讨论】:

  • 您好,您已经采用了第一种情况,当我没有输出变量时,意味着我有简单的 SELECT 记录集查询。我对此表示怀疑,mysqli_fetch_array 是否遵循我正在工作的 CakePHP 框架的标准,我也希望遵循框架的标准。案例二:我为结果选择了 vars。我会这样查询。 $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; } } 正确??
  • 好吧,CakePHP 是一个框架——它是建立在现有代码片段之上的。这并不意味着它将涵盖 PHP 基础代码的所有方面。我想说的是——你必须用“纯”PHP 编写一些东西,因为框架可能不提供这样的功能。 TLDR;这是来自 CakePHP 文档的一篇不错的文章:book.cakephp.org/2.0/en/models/retrieving-your-data.html。但是,使用非框架方法是完全正确的。 :)
  • 请检查我为最终测试用例附加的代码。
【解决方案2】:

使用以下代码调用存储过程 cakephp 3

它对我有用.....

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

【讨论】:

    【解决方案3】:

    伙计们不要打你的头

    检查那几行:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-12
      • 1970-01-01
      • 2012-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-15
      • 1970-01-01
      相关资源
      最近更新 更多