dongwen9947 2012-09-17 12:25
浏览 145
已采纳

Yii - 使用afterDelete()更新另一个表中的数据

I am using Yii afterdelete() to update the related data which is deleted in another table. Here is my code in the controller:

Controller Action

public function actionDelete($id)
{
    if(Yii::app()->request->isPostRequest)
    {
        // we only allow deletion via POST request
        $this->loadModel($id)->delete();

        // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
        if(!isset($_GET['ajax']))
            $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
    }
    else
        throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
}

Model function

  protected function afterDelete()
   {
        parent::afterDelete();
        $show_model = new Show();
        $show_model = Show::model()->findAll('tbl_season_id='.$this->id);
        $show_model->updateAll('tbl_season_id = NULL, on_season=0');

   }
  • 写回答

2条回答 默认 最新

  • dongtun3328 2012-09-18 09:56
    关注

    As @Gregor said, a good use of active record relations would make the job much easier. So, in Show model you would have something like:

         public function relations()
         {
            return array(
                'season' => array(self::BELONGS_TO, 'Season', 'tbl_season_id'),
            );
         }
    

    While in Season model you would have something like:

         public function relations()
         {
            return array(
                'shows' => array(self::HAS_MANY, 'Show', 'tbl_show_id'),
            );
         }
    

    Having relations defined, will give you the ability to do this:

         public function afterDelete()
         {
             parent::afterDelete();
             $season_shows = Season::model()->findByID($id)->shows; //using the shows relation
             foreach($season_shows as $season_show) do
             {
                $season_show->setAttributes('tbl_season_id => NULL, on_season => 0');
                $season_show->save();
             }
    
         }
    

    Hummm, but if you noticed that second line in the afterDelete which calls findByID($id) but we are inside the afterDelete and the record is actually dead (deleted)!!

    To fix this you can grab the id just before the model is deleted using a variable & abeforeDelete

        //at the begining of your model class
        $private = $cached_season_id;
        ...
        //then somewhere at the end
        ... 
        public function beforeDelete()
         {
              $this->cached_tbl_season_id = $this->id;
              return parent::beforeDelete();
         }
    

    Now if you change the id in the afterDelete to $this->cached_season_id .. it should work.

    Well, this solution is based on this yii-fourm-topic and i am not quite sure if it's going to work as it is!! So, give it a try & let us know what happens?

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

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程