Yii2: Exception 'ReflectionException' with message 'Class db does not exist'

Created on 26 Dec 2016  ·  10Comments  ·  Source: yiisoft/yii2

What steps will reproduce the problem?

i created a migration with a comand yii migration/create xxx
then i wanted to run the comand yii migrate and got the error:

`
Yii Migration Tool (based on Yii v2.0.10)

Exception 'ReflectionException' with message 'Class db does not exist'

in D:\Program Files (x86)\Ampps\www\project\advanced\vendor\yiisoft\yii2\di\Container.php:422

Stack trace:

0 D:\Program Files (x86)\Ampps\www\project\advanced\vendor\yiisoft\yii2\di\Container.php(422): ReflectionClass->__construct('db')

1 D:\Program Files (x86)\Ampps\www\project\advanced\vendor\yiisoft\yii2\di\Container.php(362): yii\di\Container->getDependencies('db')

2 D:\Program Files (x86)\Ampps\www\project\advanced\vendor\yiisoft\yii2\di\Container.php(154): yii\di\Container->build('db', Array, Array)

3 D:\Program Files (x86)\Ampps\www\project\advanced\vendor\yiisoft\yii2\di\Instance.php(157): yii\di\Container->get('db')

4 D:\Program Files (x86)\Ampps\www\project\advanced\vendor\yiisoft\yii2\di\Instance.php(131): yii\di\Instance->get(NULL)

5 D:\Program Files (x86)\Ampps\www\project\advanced\vendor\yiisoft\yii2\console\controllers\MigrateController.php(170): yii\di\Instance::ensure('db', 'yii\db\Connecti...')

6 D:\Program Files (x86)\Ampps\www\project\advanced\vendor\yiisoft\yii2\base\Controller.php(152): yii\console\controllers\MigrateController->beforeAction(Object(yii\base\InlineAction))

7 D:\Program Files (x86)\Ampps\www\project\advanced\vendor\yiisoft\yii2\console\Controller.php(128): yii\base\Controller->runAction('', Array)

8 D:\Program Files (x86)\Ampps\www\project\advanced\vendor\yiisoft\yii2\base\Module.php(454): yii\console\Controller->runAction('', Array)

9 D:\Program Files (x86)\Ampps\www\project\advanced\vendor\yiisoft\yii2\consoleApplication.php(180): yii\base\Module->runAction('migrate', Array)

10 D:\Program Files (x86)\Ampps\www\project\advanced\vendor\yiisoft\yii2\consoleApplication.php(147): yii\consoleApplication->runAction('migrate', Array)

11 D:\Program Files (x86)\Ampps\www\project\advanced\vendor\yiisoft\yii2\baseApplication.php(375): yii\consoleApplication->handleRequest(Object(yii\console\Request))

12 D:\Program Files (x86)\Ampps\www\project\advanced\vendor\yiisoft\yii2\yii(37): yii\baseApplication->run()

13 {main}

`

Additional info

| Yii version 2.0
| PHP version 5.6
| Operating system - win 10

could you help me please?

question

Most helpful comment

All 10 comments

Thank you for your question.
In order for this issue tracker to be effective, it should only contain bug reports and feature requests.

We advise you to use our community driven resources:

If you are confident that there is a bug in the framework, feel free to provide information on how to reproduce it. This issue will be closed for now.

_This is an automated comment, triggered by adding the label question._

You should configure DB connection

@SilverFire, trusy me, i've configured everything i found in advanced version.

main-local.php in directories:
common/config
console/config
environments/dev и environments/prod

where should i look further?

@SilverFire i've just installed a new project with yii2 basic.

now i did everything acording the instruction here: http://www.yiiframework.com/doc-2.0/guide-start-databases.html and here: http://www.yiiframework.com/doc-2.0/guide-db-migrations.html

file db.php contains further code:
`

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

file console.php contains further code:
`

$params = require(__DIR__ . '/params.php');
$db = require(__DIR__ . '/db.php');

$config = [
'id' => 'basic-console',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'controllerNamespace' => 'app\commands',
'components' => [
'cache' => [
'class' => 'yii\caching\FileCache',
],
'log' => [
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'db' => $db,
],
'params' => $params,
/*
'controllerMap' => [
'fixture' => [ // Fixture generation command line.
'class' => 'yii\faker\FixtureController',
],
],
*/
];

if (YII_ENV_DEV) {
// configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = [
'class' => 'yii\gii\Module',
];
}

return $config;
`

it's all full stock... Everything is stock...
P.S.: yes i'm sure that dsn, username and password are correct (i've checked it 5! Carl times)

i run bin/yii migrate/create xxx -> it works fine.
then i run bin/yii migrate -> the same. All the same.

Exception 'ReflectionException' with message 'Class db does not exist'
in D:\Program Files (x86)\Ampps\www\project\advanced\vendor\yiisoft\yii2\di\Container.php:422

I found all the classes with name db, i've checked all the namespaces, they look fine, i've changed nothing.

the app is running on apache, Ampps, php 5.6, windows 10, yii version 2.0.10

What am i doing wrong? Why is this error appearing?

@githubjeka А ты то кроме как троллить больше ничего и не умеешь... петросян блин...

Проблема решается за 5 сек. Настройкой db компонента. Либо он не настоен.
Либо файл конфига не подключен в yii.php

27 дек. 2016 г. 9:41 PM пользователь "Leonid" notifications@github.com
написал:

@githubjeka https://github.com/githubjeka А ты то кроме как троллить
больше ничего и не умеешь... петросян блин...


You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub
https://github.com/yiisoft/yii2/issues/13279#issuecomment-269365385, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AA1W-uYf27MBZ3ui-PE1xICh6SouwXfWks5rMVvdgaJpZM4LV664
.

@githubjeka, поставил basic yii2 для теста.

файл yii: (полный сток)

`/**

defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');

require(__DIR__ . '/vendor/autoload.php');
require(__DIR__ . '/vendor/yiisoft/yii2/Yii.php');

$config = require(__DIR__ . '/config/console.php');

$application = new yii\consoleApplication($config);
$exitCode = $application->run();
exit($exitCode);`

файл console.php:

`

$params = require(__DIR__ . '/params.php');
$db = require(__DIR__ . '/db.php');

$config = [
'id' => 'basic-console',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'controllerNamespace' => 'app\commands',
'components' => [
'cache' => [
'class' => 'yii\caching\FileCache',
],
'log' => [
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=yii2basic',
'username' => 'project',
'password' => 'project',
'charset' => 'utf8',
],
],
'params' => $params,
/*
'controllerMap' => [
'fixture' => [ // Fixture generation command line.
'class' => 'yii\faker\FixtureController',
],
],
*/
];

if (YII_ENV_DEV) {
// configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = [
'class' => 'yii\gii\Module',
];
}

return $config;
`

проверил, что до БД можно дотянуться вот так:
`

    $connection = Yii::$app->db;
    if ($connection)
    echo "Есть подключение к БД";
    else
    echo "Нет подключения к БД";

`
подключение к бд есть - миграция не проходит... выдает все ту же ошибку.

Есть еще идеи?... о_О Про DB конфиг я уже 100 раз слышал... могу на гит выложить вообще все что установлено всю директорию basic, которую установил...

Что здесь не так? все по инструкции сделано.

Есть тимвьёвер? и добро пожаловать в https://gitter.im/yiisoft/yii2/rus, там всё быстро решается. Здесь не место это обсуждать ....

@githubjeka да, тимвьюер есть, вечером смогу быть за компом, сможешь подключиться помочь? Ок, далее буду писать в чат =))

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SamMousa picture SamMousa  ·  55Comments

schmunk42 picture schmunk42  ·  125Comments

samdark picture samdark  ·  52Comments

alexandernst picture alexandernst  ·  163Comments

sapsxxxil picture sapsxxxil  ·  50Comments