如何创建触发器,以便在更新表中的列之前将整个旧行保存到新表中?

时间:2019-04-30 08:58:54

标签: mysql laravel triggers sql-update

我有一个team_members表,它具有以下结构,其中只有project_request_id被更新。我创建了具有相同数据结构的log_team_members,所以每当更新team_members时,我都会丢失旧数据(我需要读取这些数据),这就是为什么我想创建一个trigger的{​​{1}},它将整个before updating team_members project_request_id保存到此表中。

您能帮我从Latavel迁移中构建触发器吗?

old row

team_members更新发生的位置:

public function up()
{
    Schema::create('team_members', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('title');
        $table->string('picture');
        $table->string('email')->unique();
        $table->string('phone_number');
        $table->text('background_information')->nullable();
        $table->timestamps();
        $table->integer('project_request_id')->unsigned();
        $table->foreign('project_request_id')->references('id')->on('project_requests')->onDelete('cascade');
    });
}

public function up()
{
    Schema::create('team_members', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('title');
        $table->string('picture');
        $table->string('email')->unique();
        $table->string('phone_number');
        $table->text('background_information')->nullable();
        $table->timestamps();
        $table->integer('project_request_id')->unsigned();
        $table->foreign('project_request_id')->references('id')->on('project_requests')->onDelete('cascade');
    });
}

1 个答案:

答案 0 :(得分:0)

// use DB; 
$data = UpdateModelName::find(1);
$copy = $data->replicate()->toArray();
// store data in new table
$newTable = new NewTableModel;
$newTable->insert($copy);