duandao6414 2012-10-15 13: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 13: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条)

报告相同问题?

悬赏问题

  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化