php Laravel,从迁移创建 MySQL 触发器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27410909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:21:33  来源:igfitidea点击:

Laravel, create MySQL trigger from Migration

phplaravellaravel-5triggersmigration

提问by maytham-???????

I have created MySQL stored procedure from migration and it works just fine.

我已经从迁移中创建了 MySQL 存储过程,它工作得很好。

DB::unprepared('
    CREATE PROCEDURE sp_Create_Default_Task_1(IN _kid_id INT)
    BEGIN
        INSERT INTO tasks (kid_id, name) VALUES (_kid_id, \'daily\');
    END'
    );

Hereafter I tried to do the same to create MySQL trigger with following code

此后我尝试使用以下代码创建 MySQL 触发器

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateTrigger extends Migration {

    public function up()
    {
        DB::unprepared('
        CREATE TRIGGER tr_Task_Default AFTER INSERT ON `kids` FOR EACH ROW
            INSERT INTO tasks (`kid_id`, `name`) VALUES (NEW.id, \'Default\');
        ');
    }


    public function down()
    {
        DB::unprepared('DROP TRIGGER `tr_User_Default_Member_Role`');
    }
}

But it returns error after I run php artisan migrate

但是我运行后它返回错误 php artisan migrate

{"error":{"type":
"Symfony\Component\Debug\Exception\FatalErrorException",
"message":"Class 'CreateTriggers' not found",
"file":"C:\xampp\htdocs\dev03\vendor\laravel\framework
\src\Illuminate\Database\Migrations\Migrator.php",
"line":301}}

Question:What is going wrong?

问题:出了什么问题?

回答by maytham-???????

There was issue with class naming.

类命名存在问题。

Correct class name could help OR do as I did, Copy your working trigger code temporary in notepad/text. Delete the old migration trigger file and generate new one.

正确的类名可以帮助或像我一样做,在记事本/文本中临时复制您的工作触发代码。删除旧的迁移触发器文件并生成新的。

Note: By the way the same solution is valid for Laravel 4.x and Laravel 5.x

注意:顺便说一下,相同的解决方案适用于 Laravel 4.x 和 Laravel 5.x

In Laravel 4

在 Laravel 4

php artisan generate:migration create_trigger

In Laravel 5

在 Laravel 5

php artisan make:migration create_trigger

After it was generated I copy and paste the same Trigger code from my notepad/text and it works just fine.

生成后,我从记事本/文本中复制并粘贴相同的触发器代码,它工作得很好。

Here is the final working code for creating trigger through migration.

这是通过迁移创建触发器的最终工作代码。

it works both with RAWand UNPREPAREDmethod.

它适用于RAWUNPREPARED方法。

<?php

use Illuminate\Database\Migrations\Migration;

class CreateTrigger extends Migration {

    public function up()
    {
        DB::unprepared('
        CREATE TRIGGER tr_User_Default_Member_Role AFTER INSERT ON `users` FOR EACH ROW
            BEGIN
                INSERT INTO role_user (`role_id`, `user_id`, `created_at`, `updated_at`) 
                VALUES (3, NEW.id, now(), null);
            END
        ');
    }

    public function down()
    {
        DB::unprepared('DROP TRIGGER `tr_User_Default_Member_Role`');
    }
}

Note: This is just example to demonstrate the concept

注意:这只是演示概念的示例

回答by Yarco

Run composer dumpautoloadin the root (the same place as artisan) should make it work.

composer dumpautoload在根目录中运行(与 相同的位置artisan)应该可以使其工作。

回答by Cristian Ghirba

In Laravel 5.5 works only with DB::unprepared()method.

在 Laravel 5.5 中仅适用于DB::unprepared()方法。