A
A
Anton2016-05-17 14:04:23
Yii
Anton, 2016-05-17 14:04:23

How to get user in console command?

Hello, there is a class Accounts that is inherited from ActiveRecord when I try to do the following in the console command, the console displays this: Exception 'yii\db\Exception' with message 'could not find driver' in F:\wamp\www\blog\vendor\yiisoft \yii2\db\Connection.php:547 Stack trace: 0 F:\wamp\www\blog\vendor\yiisoft\yii2\db\Connection.php(861): yii\db\Connection->open() 1 F :\wamp\www\blog\vendor\yiisoft\yii2\db\Connection.php(848): yii\db\Connection->getMasterPdo() 2 F:\wamp\www\blog\vendor\yiisoft\yii2\db \Command.php(219): yii\db\Connection->getSlavePdo() 3 F:\wamp\www\blog\vendor\yiisoft\yii2\db\Command.php(894): yii\db\Command-> prepare(true)
$account = Accounts::findOne(["id" => 1]);
4 F:\wamp\www\blog\vendor\yiisoft\yii2\db\Command.php(376): yii\db\Command->queryInternal('fetch', NULL)
5 F:\wamp\www\blog\ vendor\yiisoft\yii2\db\Query.php(247): yii\db\Command->queryOne()
6 F:\wamp\www\blog\vendor\yiisoft\yii2\db\ActiveQuery.php(290): yii\db\Query->one(NULL)
7 F:\wamp\www\blog\vendor\yiisoft\yii2\db\BaseActiveRecord.php(107): yii\db\ActiveQuery->one()
8 F:\ wamp\www\blog\commands\RbacController.php(36): yii\db\BaseActiveRecord::findOne(Array)
9 [internal function]: app\commands\RbacController->actionTest()
10 F:\wamp\www\ blog\vendor\yiisoft\yii2\base\InlineAction.php(55): call_user_func_array(Array, Array)
11 F:\wamp\www\blog\vendor\yiisoft\yii2\base\Controller.php(154): yii\ base\InlineAction->runWithParams(Array)
12 F:\wamp\www\blog\vendor\yiisoft\yii2\console\Controller.php(119): yii\base\Controller->runAction('test', Array)
13 F:\wamp\www\blog\ vendor\yiisoft\yii2\base\Module.php(454): yii\console\Controller->runAction('test', Array)
14 F:\wamp\www\blog\vendor\yiisoft\yii2\console\Application. php(176): yii\base\Module->runAction('rbac/test', Array)
15 F:\wamp\www\blog\vendor\yiisoft\yii2\console\Application.php(143): yii\console \Application->runAction('rbac/test', Array)
16 F:\wamp\www\blog\vendor\yiisoft\yii2\base\Application.php(375): yii\console\Application->handleRequest(Object( yii\console\Request))
17 F:\wamp\www\blog\yii(20): yii\base\Application->run()
18 {main}
Console config:

<?php

Yii::setAlias('@tests', dirname(__DIR__) . '/tests/codeception');

$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',
        ],
        'authManager' => [
            'class' => 'yii\rbac\PhpManager',
        ],
        'user' => [
            'class' => 'yii\web\User',
            'enableAutoLogin' => false,
            'identityClass' => 'app\models\Accounts',
            'enableSession' => false,
        ],
        '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;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Skobkin, 2016-05-17
@skobkin

It is important to understand that PHP usually has several configs - for CLI (Command Line Interface), for mod_php in Apache, for PHP-FPM, etc.
You have only one of the configs configured correctly (Apache or PHP-FPM). Add the download of the required extensions to the CLI config.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question