Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8557245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:31:57+00:00 2026-06-11T15:31:57+00:00

I am using Yii afterdelete() to update the related data which is deleted in

  • 0

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');

   }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T15:31:58+00:00Added an answer on June 11, 2026 at 3:31 pm

    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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Yii 1.1 application development Cookbook explain a method for using data from related Active
How do I escape data being inserted into a MySQL table without using an
Using Yii framework. I have 3 models. Articles - table articles(id, name) ArticlesToAuthors -
I'm using yii active records for mysql , and i have a table where
I am using PHP Yii Framework with MongoDB(yiimongodbsuite). I have created a Model which
I'm writing an extension for the Yii framework which I have asked on here
A Yii question: The Situation: I generated a model using Yii's gii code generator
I have some forms in Yii using the following to get lists of data
I'm using Yii Framework v1.1.10r3566. I'm noticing in Firebug that the generated code is
I'm new using Yii framework. I show a list a checkbox. But it's not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.