A
A
alxsmrn2016-06-23 13:07:02
Yii
alxsmrn, 2016-06-23 13:07:02

How to transfer yii2 web controller to console?

It is necessary to transfer the web controller to the console. I created a console controller in the basic/commands folder and copied the code there. I start the controller, displays an error

Exception 'yii\db\Exception' with message 'SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)'

in /opt/lampp/htdocs/basic/vendor/yiisoft/yii2/db/Connection.php:547

Stack trace:
#0 /opt/lampp/htdocs/basic/vendor/yiisoft/yii2/db/Connection.php(858): yii\db\Connection->open()
#1 /opt/lampp/htdocs/basic/vendor/yiisoft/yii2/db/Connection.php(845): yii\db\Connection->getMasterPdo()
#2 /opt/lampp/htdocs/basic/vendor/yiisoft/yii2/db/Command.php(219): yii\db\Connection->getSlavePdo()
#3 /opt/lampp/htdocs/basic/vendor/yiisoft/yii2/db/Command.php(834): yii\db\Command->prepare(true)
#4 /opt/lampp/htdocs/basic/vendor/yiisoft/yii2/db/Command.php(362): yii\db\Command->queryInternal('fetchAll', NULL)
#5 /opt/lampp/htdocs/basic/vendor/yiisoft/yii2/db/mysql/Schema.php(198): yii\db\Command->queryAll()
#6 /opt/lampp/htdocs/basic/vendor/yiisoft/yii2/db/mysql/Schema.php(97): yii\db\mysql\Schema->findColumns(Object(yii\db\TableSchema))
#7 /opt/lampp/htdocs/basic/vendor/yiisoft/yii2/db/Schema.php(146): yii\db\mysql\Schema->loadTableSchema('news')
#8 /opt/lampp/htdocs/basic/vendor/yiisoft/yii2/db/ActiveRecord.php(296): yii\db\Schema->getTableSchema('news')
#9 /opt/lampp/htdocs/basic/vendor/yiisoft/yii2/db/ActiveRecord.php(330): yii\db\ActiveRecord::getTableSchema()
#10 /opt/lampp/htdocs/basic/vendor/yiisoft/yii2/db/BaseActiveRecord.php(422): yii\db\ActiveRecord->attributes()
#11 /opt/lampp/htdocs/basic/vendor/yiisoft/yii2/db/BaseActiveRecord.php(264): yii\db\BaseActiveRecord->hasAttribute('title')
#12 /opt/lampp/htdocs/basic/commands/HellController.php(59): yii\db\BaseActiveRecord->__set('title', '1C.Drop.1 ?????...')
#13 /opt/lampp/htdocs/basic/commands/HellController.php(113): app\commands\HellController->Fullparse('h2.post__title', '.post__body_cro...', 'a.post__title_l...', '.html_format', 'https://habraha...', '1', '???????????? ??...', '1C.Drop.1 ?????...')
#14 [internal function]: app\commands\HellController->actionIndex()
#15 /opt/lampp/htdocs/basic/vendor/yiisoft/yii2/base/InlineAction.php(55): call_user_func_array(Array, Array)
#16 /opt/lampp/htdocs/basic/vendor/yiisoft/yii2/base/Controller.php(154): yii\base\InlineAction->runWithParams(Array)
#17 /opt/lampp/htdocs/basic/vendor/yiisoft/yii2/console/Controller.php(108): yii\base\Controller->runAction('index', Array)
#18 /opt/lampp/htdocs/basic/vendor/yiisoft/yii2/base/Module.php(454): yii\console\Controller->runAction('index', Array)
#19 /opt/lampp/htdocs/basic/vendor/yiisoft/yii2/console/Application.php(167): yii\base\Module->runAction('hell/index', Array)
#20 /opt/lampp/htdocs/basic/vendor/yiisoft/yii2/console/Application.php(143): yii\console\Application->runAction('hell/index', Array)
#21 /opt/lampp/htdocs/basic/vendor/yiisoft/yii2/base/Application.php(375): yii\console\Application->handleRequest(Object(yii\console\Request))
#22 /opt/lampp/htdocs/basic/yii(20): yii\base\Application->run()
#23 {main}
. The same controller but the web works without comment. As I think, the error is that it does not see the News model. Here is the code for what I use
use yii\console\Controller;
use keltstr\simplehtmldom\SimpleHTMLDom;
use app\models\News;

Where do you need to transfer the model, or how to register the full path to it? Or is this not a bug at all?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
K
Kirill Arutyunov, 2016-06-23
@arutyunov

The problem is most likely in the configs, the error is in connecting to the database,

V
vitovt, 2017-03-13
@vitovt

Go to the common/config folder, there should be main.php or main-local.php if you are checking for localhost. Enter the database connection config there

<?php
return [
    'components' => [
        'db' => [
            'class' => 'yii\db\Connection',
            'dsn' => 'mysql:host=localhost;dbname=dbname',
            'username' => 'root',
            'password' => '',
            'charset' => 'utf8',
            'tablePrefix' => 'tbl_',
        ],
        'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            'viewPath' => '@common/mail',
            'useFileTransport' => true,
        ],
    ],
];

Sometimes it happens that it doesn’t work through the localhost console (it’s like that in my Scaleway cloud). Then you have to write
'dsn' => 'mysql:host=127.0.0.1;dbname=dbname',

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question