duandao6414 2012-10-15 05:24
浏览 226
已采纳

Yii:如何从另一个控制器动作中调用控制器动作?

When I delete a 'type' i set isActive = 0;

Every 'type' 'has many' 'causal'

So when disabling a type i want disable every causal

in type controller i'm trying this

$model = $this->loadModel($id);
$model->isActive = 0;

foreach ($model->causalsObj as $key => $causal ) {
   $causal = CausalController::delete($causal->id);
}

$model->save();

This doesn't work (PHP error during ajax call)

  • 写回答

3条回答 默认 最新

  • dongluan5740 2012-10-15 05:58
    关注

    That should go into the model, not the controller, I'd use afterSave. so in the CasualType:

    public function afterSave(){
       if(!$this->isActive){
           Casual::model()->deleteAll('type_id = '.$this->id);
       }
    
       return parent::afterSave();
    }
    

    If you don't actually mean 'delete' but deactivate you can still do this in one single query using CActiveRecord::updateAll:

    public function afterSave(){
       if(!$this->isActive){
           Casual::model()->updateAll(array('isActive' => 0), 'type_id = '.$this->id);
       }
    
       return parent::afterSave();
    }
    

    Instantiating a controller in another controller does not make sense, controllers are there to handle user requests, not to hold your business logic

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部