Mr.Falstaff
Mr.Falstaff

Reputation: 15

Yii2 exception 'yii\db\Exception' with message 'could not find driver'

I have an advanced yii2 template. I'm trying to create a console command. I've created a controller class and action incide console/controllers folder:

namespace console\controllers;

use yii\console\Controller;

class WorkModelController extends Controller
{
    public function actionValidate(){}
}

My action should connect with mysql database, select some data and do something with it. When I running the command: yii work-model/validate I get this error:

C:\OSPanel\domains\localhost>yii work-model/validate Exception 'yii\db\Exception' with message 'could not find driver'

in C:\OSPanel\domains\localhost\vendor\yiisoft\yii2\db\Connection.php:56

My console/config/main.php and main-local.php files contain next db-config:

'components' => [
        'db' => [
            'class' => 'yii\db\Connection',
            'dsn' => 'mysql:host=localhost;dbname=dbname',
            'username' => 'root',
            'password' => '',
            'charset' => 'utf8',
        ],
];

When I trying connect to the database from another part of app all works fine, but when I trying connect from console I get this error.

Please, help me slove this error.

Upvotes: 1

Views: 8736

Answers (1)

kidzen
kidzen

Reputation: 161

try to run php -me from the cli and make sure pdo_mysql is there.. if not, then u need to enable it from your php.ini config.

Sometime, when u use server stack such as lamp/xampp, u probably missed out from resync your environment path to use the same version of php.ini of your server stack. to do this, u may simply check/compare php.ini path from both phpinfo() from the browser and php -i from the cli

Upvotes: 6

Related Questions