dtihe8614 2015-02-01 06:07
浏览 61
已采纳

Yii2以编程方式迁移命令

I want to migrate up/down the database using the code instead of the console window. I have looked into the framework. I have tried the following code:

$runner = new yii\console\Application([
                                          'id'       => 'auto-migrate',
                                          'basePath' => dirname(__DIR__)
                                      ]);        

    $runner->runAction('migrate');
    ob_start();
    return htmlentities(ob_get_clean(), null, Yii::$app->charset);

It gives the Internal Server Error. And doesn't even migrates the files to the database. But if the directory doesn't exists, it creates the directory. It is behaving the way it should but if the migration file exist in that same directory it gives Internal Server Error.

  • 写回答

5条回答 默认 最新

  • douquan1015 2015-03-02 21:58
    关注

    I would probably do that over php function exec, but if you really want to do it by creating new application instance then you should be able to do it like this.

    Case 1 - not very good idea

    //Define, if you want to capture output
    defined('STDIN') or define('STDIN', fopen('php://input', 'r'));
    defined('STDOUT') or define('STDOUT', fopen('php://output', 'w'));
    //Create new console app, if you do not run it from root namespace you should use '\yii\console\Application'
    $runner = new \yii\console\Application([
        'id'       => 'auto-migrate',
        'controllerNamespace' => 'console\controllers', //for migrate command it should not matter but for other cases you must specify otherwise applocation cannot find your controllers
        'basePath' => dirname(__DIR__ . '/../../console/config'), //This must point to your console config directory, when i run this in frontend sitecontroller i must add '/../../console/config',
        'components' => [
                    'db' => [//If you want to call migrate you probably need some database component
                        'class' => 'yii\db\Connection',
                        'dsn' => 'mysql:host=localhost;dbname=mydb',
                        'username' => 'root',
                        'password' => '',
                        'charset' => 'utf8',
                    ],
                ],
    ]);        
    ob_start();
    try
    {
        $runner->runAction('migrate/up');
    }
    catch(\Exception $ex)
    {
        echo $ex->getMessage();
    }
    return htmlentities(ob_get_clean(), null, Yii::$app->charset);
    

    And result in my case:

    Yii Migration Tool (based on Yii v2.0.1) No new migration found. Your system is up-to-date.
    

    Case 2

        ob_start();
        try
        {
            $output = Array();
            exec(__DIR__ . '/../../yii migrate/up', $output);
            echo implode("
    ", $output);
        }
        catch(\Exception $ex)
        {
            echo $ex->getMessage();
        }
        return htmlentities(ob_get_clean(), null, Yii::$app->charset);
    

    And result in my case:

    Yii Migration Tool (based on Yii v2.0.1) No new migration found. Your system is up-to-date.
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了