Yii2 - Getting unknown property: yii\console\Request::userAgent and Class db does not exist

Dr Aka

I have a problem with Yii2 framework version 2.0.42.1, with a yii2 advanced app template project.

When I run the yii php script: php yii.

It returns me the following error:

Exception 'yii\base\UnknownPropertyException' with message 'Getting unknown property: yii\console\Request::userAgent'

in /usr/src/app/vendor/yiisoft/yii2/base/Component.php:155

Stack trace:
#0 /usr/src/app/common/components/VerificationNavigateur.php(23): yii\base\Component->__get('userAgent')
#1 /usr/src/app/vendor/yiisoft/yii2/base/BaseObject.php(109): common\components\VerificationNavigateur->init()
#2 /usr/src/app/common/components/AppBootstrap.php(20): yii\base\BaseObject->__construct()
#3 /usr/src/app/vendor/yiisoft/yii2/base/Application.php(333): common\components\AppBootstrap->bootstrap(Object(yii\console\Application))
#4 /usr/src/app/vendor/yiisoft/yii2/base/Application.php(279): yii\base\Application->bootstrap()
#5 /usr/src/app/vendor/yiisoft/yii2/console/Application.php(125): yii\base\Application->init()
#6 /usr/src/app/vendor/yiisoft/yii2/base/BaseObject.php(109): yii\console\Application->init()
#7 /usr/src/app/vendor/yiisoft/yii2/base/Application.php(212): yii\base\BaseObject->__construct(Array)
#8 /usr/src/app/vendor/yiisoft/yii2/console/Application.php(90): yii\base\Application->__construct(Array)
#9 /usr/src/app/yii(22): yii\console\Application->__construct(Array)

Everything was fine until I ran composer install.

Even If I try to downgrade the framework composer require yiisoft/yii2:2.0.41, that doesn't change anything.

When I try to run the php script from the vendor folder everything is fine php vendor/bin/yii

But when I try to run migrations php vendor/bin/yii migrate, this error occurs :

Yii Migration Tool (based on Yii v2.0.42.1)

Exception 'yii\di\NotInstantiableException' with message 'Failed to instantiate component or class "db".'

in /usr/src/app/vendor/yiisoft/yii2/di/Container.php:510

Caused by: Exception 'ReflectionException' with message 'Class db does not exist'

in /usr/src/app/vendor/yiisoft/yii2/di/Container.php:508

Stack trace:
#0 /usr/src/app/vendor/yiisoft/yii2/di/Container.php(508): ReflectionClass->__construct('db')
#1 /usr/src/app/vendor/yiisoft/yii2/di/Container.php(386): yii\di\Container->getDependencies('db')
#2 /usr/src/app/vendor/yiisoft/yii2/di/Container.php(171): yii\di\Container->build('db', Array, Array)
#3 /usr/src/app/vendor/yiisoft/yii2/di/Instance.php(176): yii\di\Container->get('db')
#4 /usr/src/app/vendor/yiisoft/yii2/di/Instance.php(145): yii\di\Instance->get(NULL)
#5 /usr/src/app/vendor/yiisoft/yii2/console/controllers/MigrateController.php(183): yii\di\Instance::ensure(Object(yii\di\Instance), 'yii\\db\\Connecti...')
#6 /usr/src/app/vendor/yiisoft/yii2/base/Controller.php(179): yii\console\controllers\MigrateController->beforeAction(Object(yii\base\InlineAction))
#7 /usr/src/app/vendor/yiisoft/yii2/console/Controller.php(184): yii\base\Controller->runAction('up', Array)
#8 /usr/src/app/vendor/yiisoft/yii2/base/Module.php(534): yii\console\Controller->runAction('up', Array)
#9 /usr/src/app/vendor/yiisoft/yii2/console/Application.php(181): yii\base\Module->runAction('migrate/up', Array)
#10 /usr/src/app/vendor/yiisoft/yii2/console/Application.php(148): yii\console\Application->runAction('migrate/up', Array)
#11 /usr/src/app/vendor/yiisoft/yii2/base/Application.php(392): yii\console\Application->handleRequest(Object(yii\console\Request))
#12 /usr/src/app/vendor/yiisoft/yii2/yii(37): yii\base\Application->run()
#13 {main}

My database configuration connection file didn't change, common/config/main-local.php:

<?php
return [
    'components' => [
        'db' => [
            'class' => 'yii\db\Connection',
            'dsn' => 'pgsql:host=172.21.0.3;dbname=dev',
            'username' => 'dev',
            'password' => 'dev',
            'charset' => 'utf8',
        ],
       ...

PHP version: 7.4

Linux distribution: Ubuntu 20.04

And the Yii2 forum is down for maintenance for a while now.

Michal Hynčica

The problem is in this component common\components\VerificationNavigateur.

You are accessing Yii::$app->request->userAgent in its init() method. This property is only available in yii\web\Request. In your app the component common\components\VerificationNavigateur is bootstrapped even for your console app. But in that case the property Yii::$app->request contains instance of yii\console\Request that doesn't have property userAgent.

If you don't need that component in your console app you should remove it in your console config. If you need that component in console app you should modify its init() method to check if the Yii::$app->request is instance of yii\web\Request before accessing userAgent property for example like this:

if (\Yii::$app->request instanceof \yii\web\Request) {
    // ... do something with Yii::$app->request->userAgent
}

In case of php vendor/bin/yii your app config is not used. That's why that component is not bootstrapped and you don't encounter that error. But, because the config is not loaded, there is no configuration for db component and migration is not working.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Getting unknown property Exception in yii2

Yii2 - Getting unknown property: yii\console\Application::user

yii2 class validator does not exist

Yii2 Class yii\authclient\clients\GoogleOAuth does not exist

Getting unknown property: yii\db\ActiveQuery::reg_no

Using property of parent class gives "unknown property" error in php (yii2)

Property 'do' does not exist on type 'AngularFireList<unknown>'

Yii2 Login from DB (Setting unknown property: app\models\User::password_hash)

The property "Class" does not exist in the namespace

Property 'pipe' does not exist on type 'OperatorFunction<unknown, [unknown, boolean, any]>

Typescript: Property 'DB' does not exist on type 'Global'

getting error Property '...' does not exist on type 'typeof "...."'

Getting error 'Property '...' does not exist on type 'AbstractControl'

Getting unknown property: yii\web\User::admin

class declaration - Property does not exist on type in TypeScript

Property does not exist on type Class and components angular

Error : Property 'label' does not exist on type 'unknown'.ts(2339) in reactjs

react18 - Property 'xxx' does not exist on type 'unknown' in typescript

Property 'xxx' does not exist on type 'object' when object is of unknown type

Property 'xxx' does not exist on type 'Observable<unknown>'.ts (2339)

Typescript error: Property 'clear' or 'send' does not exist on type 'unknown' in protractor

Property 'appendChild' does not exist on type 'unknown'.ts(2339)

Unknown Class – yii\base\UnknownClassException Yii2

Unknown Property and programatically adding textfields in Yii2 form

Intellisense says the property does not exist in Class1 and Class2

What is a reason of getting error: "Unknown property '' on class MyBean"?

Returning an Observable in Angular 2 App, but still getting error: "Property 'subscribe' does not exist on type 'void'

Add filter property to Yii2 yii\grid\ActionColumn class

Property 'User' does not exist on type 'typeof db' sequelize in express typescript