我正在做一个网络应用程序,用户可以在其中选择他们的假期。因为他不需要一次选择所有的星期,所以我在 HolidayPeriod 模型的 save() 方法中(在事务中)融合了连续的假期。
我的问题是,HolidayPeriod 的 rules() 中的验证之一是新时期不与任何现有时期重叠。所以我的实际代码是:
public function save($runValidation = true, $attributes = null)
{
$ts = Yii::app()->db->beginTransaction();
try {
$periods=$this->user->holidayPeriods;
foreach ($periods as $period){
if ($this->isConsecutiveWith($period)){
$this->fusion($period);
}
}
if ($result = parent::save($runValidation, $attributes)) {
....................
....................
private function fusion($period){
$this->start=date('Y-m-d',min(strtotime($this->start),strtotime($period->start)));
$this->end=date('Y-m-d',max(strtotime($this->end),strtotime($period->end)));
if (!$period->delete()){
echo "FAIL<BR>";
throw new Exception();
}else {
echo "OK<BR>";
}
}
问题在于,当调用parent::save($runValidation, $attributes)时,验证检测到已删除的期间重叠并且失败。于是我做了一个简单的测试:
$periods=$this->user->holidayPeriods;
echo count($periods);
foreach($periods as $period){
$period->delete();
}
echo count($this->user->holidayPeriods);
并且两个调用echo在开始和结束时打印相同的数字。
如何在 delete() 之后更新 $this->user->holidayPeriods?
谢谢