Yii2迁移问题 [英] Yii2 migration problems
问题描述
我第一次使用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'
what's the problem?
in /var/www/yii2.uz/vendor/yiisoft/yii2/di/Container.php:415
我的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屋!