我正在尝试调试beforeSave函数,但不知道如何正确地执行它。即使调试级别设置为2,也只是跳过var_dump和debug函数。 尝试保存时会发生什么,通知用户已保存事件,但实际上并未保存。
以下是来自'Event'模型的代码:
public function beforeSave($options = array()){
if($this->calculateWorkingHours($this->data[$this->alias])){
return false;
}else{
return true;
}
}
public function calculateWorkingHours($event){
sscanf($event['start_time'], '%d:%d:%d', $startHour, $startMin, $startSec);
sscanf($event['finish_time'], '%d:%d:%d', $finishHour, $finishMin, $finishSec);
$pauseHour = $event['pause']['hour'];
$pauseMin = $event['pause']['min'];
$startInMinutes = ($startHour * 60 + $startMin);
$finishInMinutes = ($finishHour * 60 + $finishMin);
$totalPauseMinutes = ($pauseHour * 60 + $pauseMin);
$differenceInMinutes = $finishInMinutes - $startInMinutes;
$finalHours;
$finalMinutes;
if($differenceInMinutes > 0){//possitive minutes
if($totalPauseMinutes < $differenceInMinutes){
if($event['paid']=='1'){
$finalHours = floor($differenceInMinutes / 60);
$finalMinutes = $differenceInMinutes - $finalHours * 60;
}else{//if breaks are not paid
$totalMinutes = $differenceInMinutes - $totalPauseMinutes;
$finalHours = floor($totalMinutes / 60);
$finalMinutes = $totalMinutes - $finalHours * 60;
}
$event['hours'] = $finalHours;
$event['minutes'] = $finalMinutes;
}
}else{//negative minutes
$totalMinutesPreBreak = floor(((1440 - $startInMinutes)+ $finishInMinutes) + ((1440 - $startInMinutes) + $finishInMinutes))/2;
if($totalPauseMinutes < $totalMinutesPreBreak){
if($event['paid']=='1'){//if breaks are paid
$finalHours = floor($totalMinutesPreBreak / 60);
$finalMinutes = floor($totalMinutesPreBreak - $finalHours * 60);
}else{//if breaks are not paid
$totalMinutes = $totalMinutesPreBreak - $totalPauseMinutes;
$finalHours = floor($totalMinutes / 60);
$finalMinutes = $totalMinutes - $finalHours * 60;
}
$event['hours'] = $finalHours;
$event['minutes'] = $finalMinutes;
}else{
$finalHours = floor(((1440 - $startInMinutes) + $finishInMinutes)/60);
$finalMinutes = ((1440 - $startInMinutes) + $finishInMinutes) - $finalHours * 60;
$event['hours'] = $finalHours;
$event['minutes'] = $finalMinutes;
}
}
return true;
}
对于这里可能出现的错误或者调试它的最佳方法的任何帮助或指导,我们非常感激。
答案 0 :(得分:1)
问题是你总是在calculateWorkingHours函数中返回true并在beforeSave()回调中返回false。
改变:
public function beforeSave($options = array())
{
$this->_calculateWorkingHours(); // <-- without parameters, you don't need
return true;
}
private public function _calculateWorkingHours() // <-- without parameters, you don't need
{
// all your code
// and set your data instance
$this->data[$this->alias]['hours'] = $finalHours;
$this->data[$this->alias]['minutes'] = $finalMinutes;
// don't return value this is a only format function
}