在Laravel 3中,我曾经称这样的程序,一切正常。
public function get_gen_dist($skip = 0, $take = 0) {
$countries = Country::skip($skip)->take($take)->get();
foreach ($countries as $country) {
DB::query('CALL dist_proc(' . $country->id . ');');
}
}
在Laravel 4中,我将“DB :: query”更改为“DB :: raw”,没有错误,并且没有调用过程(至少没有执行过程), 带有“DB :: raw”的var_dump'ed结果看起来
object(Illuminate\Database\Query\Expression)[269]
protected 'value' => string 'CALL dist_proc(107)' (length=19)
也尝试过:
...
foreach ($countries as $country) {
$country_id = $country->id;
$db = DB::connection()->getPdo();
$stmt = $db->prepare("CALL dist_proc(?);");
$stmt->bindValue(1, $country_id, PDO::PARAM_INT);
$stmt->execute();
}
但是在第二次循环调用中我得到了
ErrorException
Packets out of order. Expected 1 received 5. Packet size=10
尝试“$ stmt-> closeCursor();”在“$ stmt-> execute();”之后,但现在成功。
如果没有循环或者循环只运行一次,则程序将成功执行。
如何在循环中调用过程?
谢谢
答案 0 :(得分:1)
尝试以这种方式运行:
public function get_gen_dist($skip = 0, $take = 0) {
$countries = Country::skip($skip)->take($take)->get();
foreach ($countries as $country) {
DB::statement(DB::raw('CALL dist_proc(' . $country->id . ');'));
}
}