我正在使用Laravel 4和PHP来构建新的应用程序。它在运行PHP 5.4.x的开发服务器上工作正常但是我的老板坚持要运行5.3.2版本
我花了整整一天来修复所有与5.3.2一起使用的东西,几乎拥有一切,所以我想,直到我遇到下面代码的问题。
我的问题从这条线开始......
DB::transaction(function($clock_in_webcam_image) use ($clock_in_webcam_image)
我相信这种类型的代码可能不适用于此版本的PHP?如果是这种情况,我可以选择运行相同的代码或让它执行相同的操作吗?
感谢任何帮助。非常不幸的是,我的老板直接告诉我,不,他不会允许我们更新到更新的PHP,所以我现在陷入困境
// Create a new time card record when a User Clocks In
public function createTimeCard($clock_in_webcam_image) {
// Create both Timecard and timecard record tables in a Transaction
DB::transaction(
function ($clock_in_webcam_image) use ($clock_in_webcam_image) {
$timeCard = DB::table('timeclock_timecard')->insertGetId(
array(
'user_id' => $this->user->user_id,
'clock_in_datetime' => $this->dateTime->format($this->dateFormat),
'clock_in_timestamp' => $this->dateTime->getTimestamp(),
'clock_in_webcam_image' => $clock_in_webcam_image
)
);
$timeCardPunchEntry = DB::table('timeclock_punch_entry')
->insertGetId(
array(
'timecard_id' => $timeCard,
'user_id' => $this->user->user_id,
'created_at_datetime' => $this->dateTime->format($this->dateFormat),
'created_at_timestamp' => $this->dateTime->getTimestamp(),
'clock_type' => 'clock_in',
'webcam_image' => $clock_in_webcam_image
)
);
return $timeCard;
}
);
}
更新
回应bansi的评论......这就是你的意思......
DB::transaction(function() use($myModel){
$myModel->updateTable1();
$myModel->updateTable2();
})
答案 0 :(得分:4)
在PHP 5.4.0之前,您无法在anonymous function中使用$this
。虽然您可以使用use
构造将变量传递到函数范围,但有一个简单的解决方法。此外,您正在错误地使用use
构造,因为父作用域中未定义$clock_in_webcam_image
。
$user = $this->user;
$dateTime = $this->dateTime;
DB::transaction(function($clock_in_webcam_image) use ($user, $dateTime) {
// snip
array(
'user_id' => $user->user_id,
'clock_in_datetime' => $dateTime->format($this->dateFormat),
'clock_in_timestamp' => $dateTime->getTimestamp(),
'clock_in_webcam_image' => $clock_in_webcam_image
)
// snip
});
答案 1 :(得分:0)
试试这个。请检查您没有定义另一个insertTimecard
。
// Create a new time card record when a User Clocks In
public function createTimeCard($clock_in_webcam_image)
{
// Create both Timecard and timecard record tables in a Transaction
DB::transaction($this->insertTimecard($clock_in_webcam_image));
}
private function insertTimecard($clock_in_webcam_image)
{
$timeCard = DB::table('timeclock_timecard')->insertGetId(
array(
'user_id' => $this->user->user_id,
'clock_in_datetime' => $this->dateTime->format($this->dateFormat),
'clock_in_timestamp' => $this->dateTime->getTimestamp(),
'clock_in_webcam_image' => $clock_in_webcam_image
)
);
$timeCardPunchEntry = DB::table('timeclock_punch_entry')->insertGetId(
array(
'timecard_id' => $timeCard,
'user_id' => $this->user->user_id,
'created_at_datetime' => $this->dateTime->format($this->dateFormat),
'created_at_timestamp' => $this->dateTime->getTimestamp(),
'clock_type' => 'clock_in',
'webcam_image' => $clock_in_webcam_image
)
);
return $timeCard;
}