Yii2迁移问题 [英] Yii2 migration problems

查看:185
本文介绍了Yii2迁移问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次使用yii2,我想尝试yii迁移. 问题: 我创建了迁移文件

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

文件已创建.然后将新表的详细信息输入迁移文件. 当我运行php yii migrate 我遇到错误

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
有什么问题吗?

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?

我的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,
    ];

和我的迁移文件:

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

推荐答案

缺少用于控制台的数据库组件设置,请将其添加到console/config/main-local.php文件中以进行本地开发:

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.

请注意,-local文件在.gitignore列表中.

Note that -local files are in .gitignore list.

这篇关于Yii2迁移问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆