2014-11-21 26 views
0

如何避免调用afterSave用于特定功能在CakePHP中在模型执行如何避免呼叫afterSave用于特定功能执行CakePHP中

具有

公共函数afterSave(){ }

每个i我需要调用afterSave()后手动插入记录...

但是当我插入记录使用for循环。我不想叫afterSave()....

我的for循环代码

if(count($properties)>0) { 

     foreach($properties as $key => $value) { 

      $ppinsertdata = array(
          'id' => '', 
          'property_id' => $value, 
          'plan_id' => ConstPropertyPlan::Free, 
          'is_expired' => 0, 
          'expiry_date' => date('Y-m-d', strtotime("+$duration days",time())) 
         ); 

      $this->PropertyPlan->save($ppinsertdata); 
      $pplastinsertid = $this->PropertyPlan->getLastInsertID(); 

      $updateproperty['id'] = $key; 
      $updateproperty['property_plan_id'] = $pplastinsertid; 
      $updateproperty['plan_id'] = ConstPropertyPlan::Free; 
      $this->Property->save($updateproperty); 

     } 
    } 

我得到错误此行$这个 - > PropertyPlan->保存($ ppinsertdata);

如何避免调用afterSave()在这种情况下

请帮助我解决这个解决方案....

回答

1

试试这个 -

$this->PropertyPlan->save($ppinsertdata, array('callbacks' => false)); //for disabling all callbacks 

阅读本 -

callbacks Set to false to disable callbacks. Using ‘before’ or ‘after’ will enable only those callbacks

save() - cak ephp

+0

感谢您的快速响应,它工作的很好......再次感谢.... – user3101664 2014-11-21 07:31:22

+0

尽量避免在循环内使用查询。改为使用'saveMany()'或'saveAll'。 :) – 2014-11-21 07:34:00

+0

谢谢 - 我正在使用此功能执行cron作业 – user3101664 2014-11-21 07:36:05