Yii2 migration problems

◇◆丶佛笑我妖孽 提交于 2019-12-10 18:25:12

问题


I am using yii2 for the first time, and I wanna trying yii migrations. The problem: I created migration file with

php yii migrate/create new_table

file is created. then I input new table details into migration file. and when I run php yii migrate I got error

Exception 'ReflectionException' with message 'Class db does not exist'
in /var/www/yii2.uz/vendor/yiisoft/yii2/di/Container.php:415
what's the problem?

my console/config/main.php:

<?php 
$params = array_merge(
    require(__DIR__ . '/../../common/config/params.php'),
    require(__DIR__ . '/../../common/config/params-local.php'),
    require(__DIR__ . '/../../common/config/main-local.php'),
    require(__DIR__ . '/params.php'),
    require(__DIR__ . '/params-local.php')
    ); 
    return [
    'id' => 'app-console',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'controllerNamespace' => 'console\controllers',
    'components' => [
        'log' => [
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
    ],
    'params' => $params,
    ];

and my migration file:

<?php use yii\db\Schema;
      use yii\db\Migration;

      class m150727_125205_new_table extends Migration
      {
       public function up()
      {
        $this->createTable('test',[
           'id'=> Schema::TYPE_PK,
            'name'=>  Schema::TYPE_STRING
        ]);
    }

    public function down()
    {
        echo "m150727_125205_new_table cannot be reverted.\n";

        return false;
    }

回答1:


DB component setup for console is missing, add this to console/config/main-local.php file for local development:

'components' => [
    'db' => [
        'class' => 'yii\db\Connection',
        'dsn' => 'mysql:host=localhost;dbname=dbname',
        'username' => 'username',
        'password' => 'password',
        'charset' => 'utf8',
    ],
],

For production server correct this file according to db settings.

Note that -local files are in .gitignore list.




回答2:


If you're using the advanced app, you should put the following code in environment/dev/common/main-local.php to declare the db component in your application:

'components' => [
    'db' => [
        'class' => 'yii\db\Connection',
        'dsn' => 'mysql:host=localhost;dbname=dbname',
        'username' => 'username',
        'password' => 'password',
        'charset' => 'utf8',
    ],
],

If you want the configuration to actually be applied to the application in the advanced template, you should run ./yii init or php yii init command.

If you're using the basic application, you should put it in common/main-local.php directly.

It's important to put it under common, not just console, as you'll probably use the same database from both, the console and the web applications.



来源:https://stackoverflow.com/questions/31674847/yii2-migration-problems

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!