我正在尝试从Laravel 3迁移到Laravel 4
在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",没有错误,并且过程没有被调用(至少过程没有被执行)。var_dump'ed result with "DB::raw" look
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->execute();"之后执行"$stmt->closeCursor();",但现在成功了。
没有循环或如果循环只运行一次,则过程执行成功。
如何在循环中调用过程?
谢谢
试着这样运行:
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 . ');'));
}
}