Console app InvalidConfigException --- RESOLVED

Hi there.

I’m using yii 2 basic application and I’m doing a console function that will be executed by a cron.

I have the following code.

config -> console.php and web.php

    'components'  => [
        'db' => $db,
        'authManager' => [
            'class' => 'yii\rbac\DbManager',
        'user' => [
            'class' => 'app\models\User',

Console application

namespace app\commands;

use yii;
use app\components\MyComponent;
use app\models\Users;
use app\models\User;
use app\models\Wave;
use yii\console\Controller;

class WaveInactivityController extends Controller
  public $connection;

  public function actionCheck($client_id = null)
     $this->connection = MyComponent::verifyConsoleConfig();


  public function process()
    $users = Users::findOne($wave_t->ctrl_user_id);
    $user = User::findIdentity($users->id);

I get this error

>PHP Fatal error:  Uncaught yii\base\InvalidCallException: Getting write-only property: app\models\User::identity in /var/www/html/Projects/Fabrica/vendor/yiisoft/yii2/base/Component.php:151
Stack trace:
#0 /var/www/html/Projects/Fabrica/vendor/yiisoft/yii2/db/BaseActiveRecord.php(298): yii\base\Component->__get('identity')
#1 /var/www/html/Projects/Fabrica/models/CampaignCenter.php(83): yii\db\BaseActiveRecord->__get('identity')
#2 /var/www/html/Projects/Fabrica/vendor/yiisoft/yii2/db/ActiveQuery.php(320): app\models\CampaignCenter::getDb()
#3 /var/www/html/Projects/Fabrica/vendor/yiisoft/yii2/db/Query.php(274): yii\db\ActiveQuery->createCommand(NULL)
#4 /var/www/html/Projects/Fabrica/vendor/yiisoft/yii2/db/ActiveQuery.php(300): yii\db\Query->one(NULL)
#5 /var/www/html/Projects/Fabrica/vendor/yiisoft/yii2/db/ActiveRelationTrait.php(185): yii\db\ActiveQuery->one()
#6 /var/www/html/Projects/Fabrica/vendor/yiisoft/yii2/db/BaseActiveRec in /var/www/html/Projects/Fabrica/vendor/yiisoft/yii2/base/Component.php on line 151

And if i descompose this line-> Yii::$app->user->setIdentity($user); i get this message

> PHP Fatal error:  Uncaught yii\base\InvalidConfigException: yii\web\Request::cookieValidationKey must be configured with a secret key. in /var/www/html/Projects/Fabrica/vendor/yiisoft/yii2/web/Request.php:1579
Stack trace:
#0 /var/www/html/Projects/Fabrica/vendor/yiisoft/yii2/web/Request.php(1561): yii\web\Request->loadCookies()
#1 /var/www/html/Projects/Fabrica/vendor/yiisoft/yii2/base/Component.php(139): yii\web\Request->getCookies()
#2 /var/www/html/Projects/Fabrica/components/Util.php(616): yii\base\Component->__get('cookies')
#3 /var/www/html/Projects/Fabrica/models/CampaignCenter.php(84): app\components\Util::getConnection(1)
#4 /var/www/html/Projects/Fabrica/vendor/yiisoft/yii2/db/ActiveQuery.php(320): app\models\CampaignCenter::getDb()
#5 /var/www/html/Projects/Fabrica/vendor/yiisoft/yii2/db/Query.php(274): yii\db\ActiveQuery->createCommand(NULL)
#6 /var/www/html/Projects/Fabrica/vendor/yiisoft/yii2/db/ActiveQuery.php(300):  in 
 var/www/html/Projects/Fabrica/vendor/yiisoft/yii2/web/Request.php on line 1579

Method getDb()

    public static function getDb()
        $clientId = Yii::$app->user->identity->getClientId();
        return Util::getConnection($clientId);

Method getConnection()

    public static function getConnection($clientId)

        if (!empty($clientId)) {
            $cookies = Yii::$app->request->cookies;
            if ($cookies->has('statusdb')) {
                if ($cookies->getValue('statusdb')) {
                    $aDB = $cookies->getValue('db');
                    try {
                        $aDb = $aDB['dbcliente_' . $clientId];
                        $services = new \yii\di\ServiceLocator();
                        $services->clear('dbcliente_' . $clientId);
                            'dbcliente_' . $clientId => [
                                'class' => 'yii\db\Connection',
                                'dsn' => $aDb['dsn'],
                                'username' => $aDb['username'],
                                'password' => $aDb['password'],
                                'charset' => 'utf8',


                        return $services->get('dbcliente_' . $clientId);
                    } catch (\PDOException $e) {
                        throw new \yii\web\HttpException($e->getMessage(), $e->errorInfo, (int)$e->getCode(), $e);

                } else {
                    return \Yii::$app->db;
            } else {
                return \Yii::$app->db;
        } else {
            return \Yii::$app->db;


It works in this way.

In case people have their database, what they will do is look for that configuration and tell the function where they should store the information.

The user component is supposed to be an instance of yii\web\User. Most likely you want to configure your user component like this:

    'components'  => [
        'user' => [
            'class' => '\yii\web\User', // optional, this is assumed by Yii when left out
            'identityClass' => 'app\models\User',

In console.php I get this when apply -> ‘identityClass’ => ‘app\models\User’,

Exception 'yii\base\InvalidConfigException' with message 'The configuration for the "user" component must contain a "class" element.'

Yes that is because neither BaseApplication::coreComponents() nor ConsoleApplication::coreComponents() include a definition for the user component, as it is usually not used in console applications. For console.php you need to explicitly define the class key for the user component (see my post above. I marked the line as optional. But for console.php its required.). You should also disable sessions for the user component in console applications.

'user' => [
    'class' = '\yii\web\User',
    'identityClass' = 'app\models\User',
    'enableSession' => false,

P.S.: What is your rationale for using the user component in a console application? While there are some cases where this could be useful, you most likely don’t need it.

Thank you now its all right.

Some of my clients have their own databases so I have to verify the connections.