Calling Yii::$app->db from console [Solved]

What is the correct way to connect to db from the console (in the advanced template)?

I have this line:

$cmd = Yii::$app->db->createCommand($q);

The console is throwing this error:

PHP Fatal error:  Class 'console\controllers\Yii' not found in /var/www/vhosts/.../console/controllers/CitiesCsvController.php on line 28

PHP Fatal Error 'yii\base\ErrorException' with message 'Class 'console\controllers\Yii' not found'

in /var/www/vhosts/.../console/controllers/CitiesCsvController.php:28

I added the db config to console/config/main-local.php, and now I get this error:

Exception 'yii\base\UnknownPropertyException' with message 'Setting unknown property: yii\console\Application::compoonents'

in /var/www/vhosts/

Do you have Yii class imported in the CitiesCsvController.php:

use Yii;

It seems you have a typo in the config ("compoonents").

use Yii;

was the issue. I compounded the problem with the typo. Thanks for catching that!