dslh32311
dslh32311
2017-01-10 15:26

在Laravel 4迁移中创建MYSQL功能

已采纳

I am working on laravel 4.1. I want to create MYSQL function with migration but it doesn't work with php artisan migrate. even if it shows Migrated: 2017_01_10_140002_create_MYSQL_UNAVAILABLE_PRODS_FUNCTION in the console. and when i copy/past the same creation code in MYQL client, it works and the function is created. and when i run php artisan migrate:rollback the function is removed.
Any help would be appreciated.

the migration file :

<?php

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

class CreateMYSQLUNAVAILABLEPRODSFUNCTION extends Migration
{

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        $available = Constant::PRODUCT_AVAILABLE_TRUE | Constant::PRODUCT_AVAILABLE_STOCK_TRUE;

        $sql = 'DROP FUNCTION IF EXISTS UNAVAILABLE_PRODS;
                DELIMITER $$
                CREATE FUNCTION UNAVAILABLE_PRODS(CONFIG_ID INT, PROD_TYPE INT)
                  RETURNS TEXT
                  LANGUAGE SQL
                BEGIN
                    DECLARE RES TEXT;

                    SET GROUP_CONCAT_MAX_LEN = 2048;

                    SET RES = (SELECT GROUP_CONCAT(ID SEPARATOR \'|\') FROM PRODUCT_BASE WHERE `PRODUCT_TYPE` & PROD_TYPE AND `PRODUCT_BASE`.`AVAILABLE` <> ' . $available . ');

                    RETURN CONCAT(CONFIG_ID, \'=(\', RES, \')\');
                END;
                $$
                DELIMITER ;
                ';

        DB::unprepared($sql);
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        DB::unprepared('DROP FUNCTION IF EXISTS UNAVAILABLE_PRODS;');
    }

}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dpjs2005 dpjs2005 4年前

    I figured out what the problem was, it's the delimiters. it worked after i removed the lignes DELIMITER $$ , $$ and DELIMITER ;

    点赞 评论 复制链接分享
  • duanlan6259 duanlan6259 4年前

    I think the mistake is here \'|\') this quotes are outside of your sql(there are not green). So php tries to execute them in some way screenshot of your code

    点赞 评论 复制链接分享

相关推荐