dounangshen6553 2012-05-19 23:29
浏览 24
已采纳

Yii交易和验证问题

I'm doing a webapp where the user can choose their holiday periods. Since he doesn't need to select all the weeks at once I'm fusioning consecutive holiday periods in the save() method of the HolidayPeriod model (within a transaction).

My problem is that one of the validation in the rules() of HolidayPeriod is that the new period doesn't overlap with any existing period. So my actual code is:

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>";          
    }   
}

The problem there is that when calling parent::save($runValidation, $attributes) the validation detects the deleted periods as overlapped and it fails. So I made a simple test:

 $periods=$this->user->holidayPeriods;
 echo count($periods);
 foreach($periods as $period){
     $period->delete();
 }
 echo count($this->user->holidayPeriods);

And both calls to echo print the same number at the start and at the end.

How can I make the $this->user->holidayPeriods to be updated after the delete()?

Thanks

  • 写回答

2条回答 默认 最新

  • drnl10253 2012-05-20 04:21
    关注

    How can I make the $this->user->holidayPeriods to be updated after the delete()?

    (Assuming this is a relation defined in relations()) You can:

    • unset($this->user->holidayPeriods) - the next time you access $this->user->holidayPeriods, it will be loaded from the DB.
    • $this->user->getRelated('holidayPeriods', true) - forces the holidayPeriods relation to be refreshed from the DB right there.
    • Avoid the refresh from DB entirely:
      
      $periods = $this->user->holidayPeriods;
      foreach ($periods as $ix => $period){
        if ($this->isConsecutiveWith($period)){
          $this->fusion($period);
          unset($periods[$ix]);
        }
      }
      $periods = array_values($periods); // fix period indexes
      unset($this->user->holidayPeriods);
      $this->user->addRelatedRecord('holidayPeriods', $periods, false);
      
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line