Dynamic migration Yii2



  • There is a project where the user is registered. A separate database of mysql with tables on the server is being established for successful user registration. Since I'm dealing with Yii2 Framworth, I don't know how to do it yet. To date, I have 25 migrations for a new user database. But I want to automate this process every time I register the user manually.

    Question: I can find out the name of the new database. How do we make migration so that we know the name of the database to turn some migration into it?



  • If in migration classes in the method init() There is no strict definition of a specific base, but by causing a commandu migrate, you may indicate the op. --dbin which you can point to the connection to the appropriate database.

    For example: yii migrate --db=db2 - Use migration to the database db2

    It won't work if it's in the method. init() specific base. For example:

    public function init()
    {
        $this->db = 'db3';
        parent::init();
    }
    

    In your case, I understand that we need to start migration immediately after successful registration. How to start a migration commanduce from the application, read here: https://stackoverflow.com/questions/27983169/how-to-handle-yii2-migrations-on-hosts-without-console-access And here: http://www.yiiframework.com/forum/index.php/topic/60000-run-migrate-command-in-a-function-in-yii2/ ♪

    And that's what you need to do.I'm starting to think that you've learned the references and that it's a piece of the code and where to put it in you already understand.😞

    new \yii\console\Application([
        'id'            => 'Command runner',
        'basePath'      => '@app',
        'components'    => [
            'db' => ... /* тут нужно будет зарегистрировать компонент подключения 
    к только что созданной базе данных. Зная ее имя, с этим не должно возникнуть трудностей */
    
    ],
    

    ]);




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2