我正在尝试根据 laravel 文件中第一个存储过程的成功和输出正确执行第二个存储过程。
我目前正在成功执行这个(插入记录并在 $out 2 中返回记录的 ID,这就是我想要的(
function firstRecord($firstName, $lastName, $email, $customer, $code, $auth)
{
$stmt = DB::connection()->getPdo()->prepare('CALL SCHEMA.INSERTRECORD(?,?,null,null,null,null,null,null,?,?,?,?)');
$stmt->bindParam(1, $firstName, PDO::PARAM_STR);
$stmt->bindParam(2, $lastName, PDO::PARAM_STR);
$stmt->bindParam(3, $email, PDO::PARAM_STR);
$stmt->bindParam(4, $customer, PDO::PARAM_STR);
$stmt->bindParam(5, $code, PDO::PARAM_STR);
$stmt->bindParam(6, $out2, PDO::PARAM_STR, 20);
$stmt->execute();
}
现在,当该值执行时,我需要将一些值(auth,out2和电子邮件(发送到执行另一个过程的另一个函数中,并且所有4个参数都需要是字符串:
function secondRecord($out2, $email, $auth)
{
$type = 'web';
$userStmt = DB::connection()->getPdo()->prepare('call SCHEMA.INSERTRECORDTWO(?,?,?,?)');
$userStmt->bindParam(1, $out2, PDO::PARAM_STR);
$userStmt->bindParam(2, $email, PDO::PARAM_STR);
$userStmt->bindParam(3, $auth, PDO::PARAM_STR, 2500);
$userStmt->bindParam(4, $type, PDO::PARAM_STR, 20);
$userStmt->execute();
}
在第一个过程/函数成功后,我应该如何使用这些值正确调用第二个函数?
考虑到您调用存储函数的特定要求,您可以使用以下代码片段:
请注意,我使用的是维护起来更干净的命名绑定。此外,如果您的存储过程正在返回(选择(某些内容,您可以使用DB::select()
将其作为输出。
我故意抓住QueryException
因为如果您的数据库没有调用过程或传递了无效的参数,您可以在那里处理这些参数
public function firstRecord($firstName, $lastName, $email, $customer, $code, $auth)
{
try{
$insertion = DB::select( DB::raw('CALL SCHEMA.INSERTRECORONE(:firstname, :lastname, null, null, null, null, null, null, :email, :customer, :code, :out2)', [
'firstName' => $firstName,
'lastName' => $lastName,
'email' => $email,
'customer' => $customer,
'code' => $code,
'out2' => $out2
]));
$this->secondRecord($out2, $email, $auth, $insertion)
return $insertion;
}
catch(QueryException $e){
throw new Exception('Unable to insert records : ' . $e->getMessage());
}
}
private function secondRecord($out2, $email, $auth, $insertion)
{
try{
$insertion = DB::select( DB::raw('CALL SCHEMA.INSERTRECORDTWO(:out2, :email, :auth, :type)', [
'email' => $email,
'auth' => $auth,
'out2' => $out2,
'type' = 'web'
]));
return $insertion;
}
catch(QueryException $e){
throw new Exception('Unable to insert records in second function : ' . $e->getMessage());
}
}
但是,我会说laravel使用雄辩的模型做这些事情非常好,除非您有非常具体的理由减少MySQL中的语句解析时间并使用存储函数/过程调用。