私は、ユーザーが休暇期間を選択できるWebアプリを作成しています。彼は一度にすべての週を選択する必要がないので、HolidayPeriodモデルのsave()メソッドで(トランザクション内で)連続する休日期間を融合しています。
私の問題は、HolidayPeriodのrules()の検証の1つが、新しい期間が既存の期間と重複していないことです。したがって、私の実際のコードは次のとおりです。
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);
そして、エコーの両方の呼び出しは、開始時と終了時に同じ番号を出力します。
delete()の後に$ this-> user-> holidayPeriodsを更新するにはどうすればよいですか?
ありがとう