Laravel 5 - 找不到 MySQL 存储过程

Laravel 5 - Cannot find MySQL Stored Procedure

问题: 将我的 L4 代码转换为 L5.2 并收到以下错误:SQLSTATE[42000]:语法错误或访问冲突:1305 FUNCTION cr_debug.ENTER_MODULE2 不存在 (SQL: 调用 POPULATE_DAYS_TABLE(20, "01/29/2016")) & SQLSTATE[42000]:语法错误或访问冲突:1305 FUNCTION cr_debug.ENTER_MODULE2 不存在

更新: 我不知道函数 FUNCTION cr_debug.ENTER_MODULE2 是什么。我不知道那是什么。这似乎是导致问题的原因,因为 MySQL 中不存在该函数,我也不知道它来自哪里。在我的 Laravel 5.2 安装中使用 Agent Ransack 我只找到提到 ENTER_MODULE2 的日志文件并在我的 Laravel 4 安装中为 ENTER_MODULE2 进行文本搜索我只找到 SQL 转储提到该文本。下面列出了该转储。 我确实在我的存储过程中找到了对 ENTER_MODULE2 的引用。正在查看,稍后会 post 返回。

SQL 转储:

DECLARE cr_stack_depth INTEGER DEFAULT cr_debug.ENTER_MODULE2('FIRST_DAY', 'contracts', 8, 100430)/*[cr_debug.1]*/;

  CALL cr_debug.UPDATE_WATCH3('`firstday`', `firstday`, 'DATE', cr_stack_depth)/*[cr_debug.2]*/;

CALL cr_debug.TRACE(4, 4, 0, 5, cr_stack_depth)/*[cr_debug.2]*/;

CALL cr_debug.TRACE(5, 5, 2, 67, cr_stack_depth)/*[cr_debug.2]*/;

SET @devart_debug_ret = ADDDATE(LAST_DAY(SUBDATE(firstday, INTERVAL 1 MONTH)), 1)/*[cr_debug.2]*/;

CALL cr_debug.LEAVE_MODULE(cr_stack_depth - 1)/*[cr_debug.2]*/;

RETURN @devart_debug_ret;

CALL cr_debug.TRACE(6, 6, 0, 3, cr_stack_depth)/*[cr_debug.2]*/;

CALL cr_debug.LEAVE_MODULE(cr_stack_depth - 1)/*[cr_debug.2]*/;

END ;;
DELIMITER ;

已尝试: 我搜索了 Whosebug 和 Google 以了解发生了什么。我试图将调用更改为 EXEC,这造成了另一个错误,指出我的 MySQL 版本不支持调用。我两周前刚刚安装了最新版本 MySQL。我试图命名过程调用无济于事。我已经从存储过程中的函数调用中删除了所有空格(示例:IF (var) 现在是 IF(var) 或函数 (var) 现在是 function(var)。我删除了存储过程并收到以下不同的错误: SQLSTATE[42000]:语法错误或访问冲突:1305 PROCEDURE contracts.POPULATE_DAYS_TABLE 不存在(SQL:调用 POPULATE_DAYS_TABLE(29,“01/29/2016”) )

请求:请协助找出问题所在,并在可能的情况下用示例详细解释我做错了什么。

注意事项: 为了简洁起见,我已经严重缩短了 ContractController.php 文件。如果您需要查看存储过程,我也可以显示它们。存储过程在 MySQL 中。这适用于 Laravel 4.

调试信息: 第 1 个错误,共 2 个 Connection.php 第 390 行中的 PDOException: SQLSTATE[42000]:语法错误或访问冲突:1305 FUNCTION cr_debug。ENTER_MODULE2 不存在

in Connection.php line 390
at PDOStatement->execute(array()) in Connection.php line 390
at Connection->Illuminate\Database\{closure}(object(MySqlConnection), 'call POPULATE_DAYS_TABLE(20, "01/29/2016")', array()) in Connection.php line 644
at Connection->runQueryCallback('call POPULATE_DAYS_TABLE(20, "01/29/2016")', array(), object(Closure)) in Connection.php line 611
at Connection->run('call POPULATE_DAYS_TABLE(20, "01/29/2016")', array(), object(Closure)) in Connection.php line 391
at Connection->statement('call POPULATE_DAYS_TABLE(20, "01/29/2016")')
at call_user_func_array(array(object(MySqlConnection), 'statement'), array('call POPULATE_DAYS_TABLE(20, "01/29/2016")')) in DatabaseManager.php line 317
at DatabaseManager->__call('statement', array('call POPULATE_DAYS_TABLE(20, "01/29/2016")')) in Facade.php line 218
at Facade::__callStatic('statement', array('call POPULATE_DAYS_TABLE(20, "01/29/2016")')) in computer.php line 15
at computer::storedProcedureCall('20', '01/29/2016') in ContractController.php line 56
at ContractController->store()
at call_user_func_array(array(object(ContractController), 'store'), array()) in Controller.php line 76
at Controller->callAction('store', array()) in ControllerDispatcher.php line 146
at ControllerDispatcher->call(object(ContractController), object(Route), 'store') in ControllerDispatcher.php line 94
at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 96
at ControllerDispatcher->callWithinStack(object(ContractController), object(Route), object(Request), 'store') in ControllerDispatcher.php line 54
at ControllerDispatcher->dispatch(object(Route), object(Request), 'App\Http\Controllers\ContractController', 'store') in Route.php line 174
at Route->runController(object(Request)) in Route.php line 140
at Route->run(object(Request)) in Router.php line 703
at Router->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Router.php line 705
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 678
at Router->dispatchToRoute(object(Request)) in Router.php line 654
at Router->dispatch(object(Request)) in Kernel.php line 246
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Kernel.php line 132
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99
at Kernel->handle(object(Request)) in index.php line 53

第 2 个错误,共 2 个 Connection.php 第 651 行中的 QueryException: SQLSTATE[42000]: 语法错误或访问冲突: 1305 FUNCTION cr_debug.ENTER_MODULE2 不存在 (SQL: call POPULATE_DAYS_TABLE(20, "01 /29/2016"))

in Connection.php line 651
at Connection->runQueryCallback('call POPULATE_DAYS_TABLE(20, "01/29/2016")', array(), object(Closure)) in Connection.php line 611
at Connection->run('call POPULATE_DAYS_TABLE(20, "01/29/2016")', array(), object(Closure)) in Connection.php line 391
at Connection->statement('call POPULATE_DAYS_TABLE(20, "01/29/2016")')
at call_user_func_array(array(object(MySqlConnection), 'statement'), array('call POPULATE_DAYS_TABLE(20, "01/29/2016")')) in DatabaseManager.php line 317
at DatabaseManager->__call('statement', array('call POPULATE_DAYS_TABLE(20, "01/29/2016")')) in Facade.php line 218
at Facade::__callStatic('statement', array('call POPULATE_DAYS_TABLE(20, "01/29/2016")')) in computer.php line 15
at computer::storedProcedureCall('20', '01/29/2016') in ContractController.php line 56
at ContractController->store()
at call_user_func_array(array(object(ContractController), 'store'), array()) in Controller.php line 76
at Controller->callAction('store', array()) in ControllerDispatcher.php line 146
at ControllerDispatcher->call(object(ContractController), object(Route), 'store') in ControllerDispatcher.php line 94
at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 96
at ControllerDispatcher->callWithinStack(object(ContractController), object(Route), object(Request), 'store') in ControllerDispatcher.php line 54
at ControllerDispatcher->dispatch(object(Route), object(Request), 'App\Http\Controllers\ContractController', 'store') in Route.php line 174
at Route->runController(object(Request)) in Route.php line 140
at Route->run(object(Request)) in Router.php line 703
at Router->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Router.php line 705
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 678
at Router->dispatchToRoute(object(Request)) in Router.php line 654
at Router->dispatch(object(Request)) in Kernel.php line 246
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Kernel.php line 132
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99
at Kernel->handle(object(Request)) in index.php line 53

ContractController.php:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Library\customer;
use App\Library\computer;

class ContractController extends Controller
{
    Protected $layout = 'master';
    public function store()
    {
        // Receive input from Form
        $input = \Input::only([
            'contractterm_id', 'businesstype_id', 'company', 'bcity', 'bphone', 'bstate', 'bstraddr',
            'bzip', 'firstname', 'lastname', 'mobile', 'hcity',
            'hphone', 'hstate', 'hstraddr', 'hzip'
        ]);

        $morepcs_array = \Input::only('addtpcmake','addtpcmodel','addtpcserial','addtpcname');
    /*  
        // Debugging Code
        foreach ($morepcs_array as $textbox_name => $textbox)
            {
                echo "<br><br>".$textbox_name;
                echo "<br>textbox quantity: ".sizeof($textbox);
                foreach($textbox as $value) {
                    if ($value == NULL) {
                        echo "<br>NULL";
                    }
                    else {
                        echo "<br>".$value;
                    }
                }
            }
    */  
        $customer = new customer($input); // Create new customer object. Store $input into this object.
        $computer = new computer; // Create new computer object
        $computer->addtpcs = \Input::get('addtpcs'); // Get the form data for addtpcs and prepare to store it in a database table named addtpcs.
        $computer->save(); // Save the information into the database table addtpcs from the computer object.
        $customer->computer()->associate($computer); // Using the associate function, store the id from the computer table in the database to the customer_id table in the database.
        $customer->save(); // send all of the data to the customer table in the database.

        // $startdate = new day;
        $startdate = \Input::get('contract_date');
        // $customer->startdate()->save($startdate);

        //Log::info('$startdate from controller before it is passed to the StoredProcedureCall method: ' . $startdate);
        //Log::info('$customer->id from controller before it is passed to the StoredProcedureCall method: ' . $customer->id);
        //Computer::storedProcedureCall($customer->id,$startdate);
        //$days = Day::all()->last(); // Never do this.  For testing purposes only.
        // Figure out total contract cost based upon Contract Term and Business Type
        computer::storedProcedureCall($customer->id,$startdate); // Call the MySQL stored procedure.
}

computer.php:

Class computer extends \Eloquent {
 protected $guarded = array();

 public function customer() {
    return $this->hasMany('App\Library\customer');
 }

 public static function storedProcedureCall($customer, $contract_date) {
         //Log::info('$contract_date after it is received from the controller: ' . $contract_date);
         //Log::info('$data after it is received from the controller: ' . $data);
         return \DB::statement('call POPULATE_DAYS_TABLE(' . $customer . ', "'.$contract_date.'")');
    }
 public static $rules = array();
  }

错误日志:

[2016-01-31 16:36:20] local.ERROR: PDOException: SQLSTATE[42000]: Syntax error or access violation: 1305 FUNCTION cr_debug.ENTER_MODULE2 does not exist in C:\Apache24\B2B_Contracts\vendor\laravel\framework\src\Illuminate\Database\Connection.php:390
Stack trace:
#0 C:\Apache24\B2B_Contracts\vendor\laravel\framework\src\Illuminate\Database\Connection.php(390): PDOStatement->execute(Array)
#1 C:\Apache24\B2B_Contracts\vendor\laravel\framework\src\Illuminate\Database\Connection.php(644): Illuminate\Database\Connection->Illuminate\Database\{closure}(Object(Illuminate\Database\MySqlConnection), 'call POPULATE_D...', Array)
#2 C:\Apache24\B2B_Contracts\vendor\laravel\framework\src\Illuminate\Database\Connection.php(611): Illuminate\Database\Connection->runQueryCallback('call POPULATE_D...', Array, Object(Closure))
#3 C:\Apache24\B2B_Contracts\vendor\laravel\framework\src\Illuminate\Database\Connection.php(391): Illuminate\Database\Connection->run('call POPULATE_D...', Array, Object(Closure))
#4 [internal function]: Illuminate\Database\Connection->statement('call POPULATE_D...')

更多错误 - 相同的日志文件:

Next Illuminate\Database\QueryException: SQLSTATE[42000]: Syntax error or access violation: 1305 FUNCTION cr_debug.ENTER_MODULE2 does not exist (SQL: call POPULATE_DAYS_TABLE(23, "01/29/2016")) in C:\Apache24\B2B_Contracts\vendor\laravel\framework\src\Illuminate\Database\Connection.php:651
Stack trace:
#0 C:\Apache24\B2B_Contracts\vendor\laravel\framework\src\Illuminate\Database\Connection.php(611): Illuminate\Database\Connection->runQueryCallback('call POPULATE_D...', Array, Object(Closure))
#1 C:\Apache24\B2B_Contracts\vendor\laravel\framework\src\Illuminate\Database\Connection.php(391): Illuminate\Database\Connection->run('call POPULATE_D...', Array, Object(Closure))
#2 [internal function]: Illuminate\Database\Connection->statement('call POPULATE_D...')
#3 C:\Apache24\B2B_Contracts\vendor\laravel\framework\src\Illuminate\Database\DatabaseManager.php(317): call_user_func_array(Array, Array)

真的很奇怪,但是不知何故 MySQL 创建了一堆函数,这些函数被添加到我的存储过程中,导致它崩溃。

更新 02/01/16: Shadow 指出额外的调试来自 dbforge studio,我确实在几年前下载它来调试存储过程。答案仍然是一样的。如果您 运行 遇到此问题,请从备份中恢复、删除调试垃圾或重新安装调试软件。

这方面的一个例子是存储过程应该看起来像:

真正的存储过程

BEGIN
SET contract_date = STR_TO_DATE(contract_date, '%m/%d/%Y'); /* Changes parameter sent it to an actual date */
SELECT DATE_FORMAT(contract_date, '%Y-%m-%d') INTO @start_date; /* Store proper date format for Contract End Dates */

已修改 MYSQL 存储过程

BEGIN
DECLARE cr_stack_depth INTEGER DEFAULT cr_debug.ENTER_MODULE2('POPULATE_DAYS_TABLE', 'contracts', 7, 100430)/*[cr_debug.1]*/;
CALL cr_debug.UPDATE_WATCH3('`customer_id`', `customer_id`, 'INT', cr_stack_depth)/*[cr_debug.2]*/;
CALL cr_debug.UPDATE_WATCH3('`contract_date`', `contract_date`, 'TEXT', cr_stack_depth)/*[cr_debug.2]*/;
CALL cr_debug.TRACE(4, 4, 0, 5, cr_stack_depth)/*[cr_debug.2]*/;
CALL cr_debug.TRACE(5, 5, 0, 59, cr_stack_depth)/*[cr_debug.2]*/;
SET contract_date = STR_TO_DATE(contract_date, '%m/%d/%Y');
CALL cr_debug.UPDATE_WATCH3('contract_date', contract_date, '', cr_stack_depth)/*[cr_debug.1]*/; /* Changes parameter sent it to an actual date */
CALL cr_debug.TRACE(6, 6, 0, 63, cr_stack_depth)/*[cr_debug.2]*/;
SELECT DATE_FORMAT(contract_date, '%Y-%m-%d') INTO @start_date;

解决方法是删除 MySQL 中的存储过程,然后从我的备份中重新导入它们。如果您没有备份,那么您将不得不手动删除添加到其中的所有垃圾。

由于对存储过程的调用很奇怪,您可以尝试将其传递到底层 PDO 对象中,例如..

$db = \DB::connection()->getPdo();
$stmt=$db->prepare($sql);
$stmt->bindParams($params);
$stmt->execute();

就像您在本地 PHP 中所做的那样。为简洁起见进行了编辑,但我希望你能理解