Insert rows in table2 when row deleted in table1

Hi All,

I have the following situation:

Course

-idCourse

-title

-description

Participant

-idParticipant

-name

-…

Register

-id

-idCourse

-idParticipant

Pool

-idPool

-idCourse

-idParticipant

I want to achieve this: When deleting a Course, I want to find all the participant which where registered for this Course (in Register table) and put them in the Pool table.

How can I do this? Please help

Override afterDelete method of Course so you can move all partecipiant to pool table.

I am doing the following but no record is inserted in Pool after record deleted in course…

In Course model:




        private $id_deleted;


	public function beforeDelete(){

		$this->id_deleted=$this->idCourse;

		return parent::beforeDelete();

	}

	

	protected function afterDelete(){

		$pool=new Pool();

		$pool->idCourse=$this->id_deleted;

		$pool->save();

		parent::afterDelete();

	}



But if you refer to idCourse in Pool’s record and then delete course record, you lose referential integrity between Pool and Course tables.

Anyway at this point, you have to put code in beforeDelete(), so:




public function beforeDelete()

{

    $pool=new Pool();

    $pool->idCourse=$this->idCourse;

    $saveReturn = $pool->save();


    if($saveReturn == false)

    {

        /* Uncomment next two lines if only save() new Pool record not works. */

        // var_dump($pool->errors);

        // exit;

    }

    return parent::beforeDelete();

}



If ($saveReturn = false), Pool new record has not been saved. Probably there are validation errors that you can see seeing content of errors property.

Thank you Fabrizio, it worked. The problem was that idParticipant could not be left blank…

I don’t understand this because I set it in db that it can be NULL…anyway I did $pool->idParticipant=2

just for testing and it works.

No problem about referential integrity because in my real application I don’t have a relationship between Pool and Course.

The structure is different. Anyway now I have a different problem:

It works fine with beforeDelete(), but in my app I don’t want to do a hard delete:


$this->loadModel($id)->delete();

What I want to do is in actionDelete() just change a flag to 1 if user deletes a record:




        public function actionDelete($id)

	{

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

    	        $model->isDeleted=1;

    	        $model->save();


                ...

        }



In this case beforeDelete() functionality doesn’t work since I am not using delete().

What can I do?

Thank you in advance.

You can create a method in your model that includes beforeDelete() content

and call it in actionDelete(), so:

In model:




public function createaPoolRecord()

{

    $pool=new Pool();

    $pool->idCourse=$this->idCourse;

    $saveReturn = $pool->save();

}



In controller:




        public function actionDelete($id)

        {

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

                $model->isDeleted=1;

                if ($model->save())

                {

                        $this->createPoolRecord();

                }

              

        }



If you want you can also extends beforeSave() method in your model, as you have overridden beforeDelete().

http://www.yiiframework.com/doc-2.0/yii-db-baseactiverecord.html#beforeSave()-detail

Cool…thank you for your help