Yii Framework Forum: Insert rows in table2 when row deleted in table1 - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

Insert rows in table2 when row deleted in table1 Rate Topic: -----

#1 User is offline   erand 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 136
  • Joined: 10-December 14

Posted 04 January 2015 - 05:08 AM

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
0

#2 User is offline   Fabrizio Caldarelli 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,047
  • Joined: 21-March 13
  • Location:Rome, Italy

Posted 04 January 2015 - 04:18 PM

Override afterDelete method of Course so you can move all partecipiant to pool table.
Yii2 by example: my book about Yii2
https://www.packtpub...nt/yii2-example
0

#3 User is offline   erand 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 136
  • Joined: 10-December 14

Posted 04 January 2015 - 07:03 PM

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();
	}

0

#4 User is offline   Fabrizio Caldarelli 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,047
  • Joined: 21-March 13
  • Location:Rome, Italy

Posted 04 January 2015 - 07:18 PM

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.
Yii2 by example: my book about Yii2
https://www.packtpub...nt/yii2-example
1

#5 User is offline   erand 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 136
  • Joined: 10-December 14

Posted 05 January 2015 - 03:39 AM

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.
0

#6 User is offline   Fabrizio Caldarelli 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,047
  • Joined: 21-March 13
  • Location:Rome, Italy

Posted 05 January 2015 - 03:49 AM

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.yiiframew...ve%28%29-detail
Yii2 by example: my book about Yii2
https://www.packtpub...nt/yii2-example
1

#7 User is offline   erand 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 136
  • Joined: 10-December 14

Posted 05 January 2015 - 08:00 AM

Cool...thank you for your help
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users