Faradox
Faradox

Reputation: 179

Yii2 migration problems

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;
    }

Upvotes: 4

Views: 5300

Answers (2)

Nico Savini
Nico Savini

Reputation: 487

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.

Upvotes: 0

arogachev
arogachev

Reputation: 33548

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.

Upvotes: 5

Related Questions