有没有办法在Laravel 4迁移中生成存储的MYSQL过程?
例如,这是一个简单的过程生成查询,以字符串形式存储(通过Heredoc)
$query = <<<SQL DELIMITER $$ DROP PROCEDURE IF EXISTS test$$ CREATE PROCEDURE test() BEGIN INSERT INTO `test_table`(`name`) VALUES('test'); END$$ DELIMITER ; SQL; DB:statement(DB::RAW($query));
在迁移up()函数中运行此命令时,出现以下错误:
up()
您的代码有两个主要问题
DELIMITER
DB::statement
CREATE PROCEDURE
Connection
exec()
DB::connection()->getPdo()->exec()
话虽如此,虚tags表的样本迁移可能看起来像这样
tags
class CreateTagsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('tags', function($table){ $table->increments('id'); $table->string('name')->unique(); }); $sql = <<<SQL DROP PROCEDURE IF EXISTS sp_insert_tag; CREATE PROCEDURE sp_insert_tag(IN _name VARCHAR(32)) BEGIN INSERT INTO `tags`(`name`) VALUES(_name); END SQL; DB::connection()->getPdo()->exec($sql); } /** * Reverse the migrations. * * @return void */ public function down() { $sql = "DROP PROCEDURE IF EXISTS sp_insert_tag"; DB::connection()->getPdo()->exec($sql); Schema::drop('tags'); } }