简体   繁体   中英

Insert rows in table2 when row deleted in table1

How can I do the following in Yii:

I have the following 4 tables:

**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

First: you need to have a model for every table you want to manage. Second: You need to have all models related depending on your database design. Third: You need to override beforeDelete or create moveToPool function in the model that will be deleted.

Models

class Course extends CActiveRecord {

    public function tableName() {
        return 'course';
    }

    public function relations() {
        return array(
            'registers'=>array(self::HAS_MANY, 'Register', 'idCourse'),
            'pool'=>array(self::HAS_MANY, 'Pool', 'idCourse'),
        );
    }
}

class Participant extends CActiveRecord {

    public function tableName() {
        return 'participant';
    }

    public function relations() {
        return array(
            'registers'=>array(self::HAS_MANY, 'Register', 'idParticipant'),
            'pool'=>array(self::HAS_MANY, 'Pool', 'idParticipant'),
        );
    }
}

class Register extends CActiveRecord {

    public function tableName() {
        return 'register';
    }

    public function relations() {
        return array(
            'register'=>array(self::HAS_ONE, 'Course', 'idCourse'),
            'pool'=>array(self::HAS_ONE, 'Participant', 'idParticipant'),
        );
    }
}

class Pool extends CActiveRecord {

    public function tableName() {
        return 'pool';
    }

    public function relations() {
        return array(
            'register'=>array(self::HAS_ONE, 'Course', 'idCourse'),
            'pool'=>array(self::HAS_ONE, 'Participant', 'idParticipant'),
        );
    }
}

beforeDelete()

This function must be located in Course model.

public function beforeDelete() {
    $models = $this->registers;

    foreach ($models as model) {
       $pool =  new Pool();
       $pool->idCourse = $this->idCourse;
       $pool->idParticipant = $model->idParticipant;
       $pool->save();
    }
    //DO NOT RETURN parent::beforeDelete() or true. 
    //if your database tables are connected in a relationship, 
    //Yii will try to delete the record, a foreign key violation will be triggered
}

My Recommendation

You cant delete the record while you have its ID in another table as a foreign key, instead of overriding beforeDelete you can create function moveToPool and use the same code I showed previously.

The simplest way is to create a trigger for the DELETE statement. See here a tutorial about creating triggers: http://code.tutsplus.com/articles/introduction-to-mysql-triggers--net-12226

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM