Yii控制台命令无法连接到数据库

时间:2017-01-25 12:49:10

标签: php yii console

我试图在Yii中设置一个控制台命令,并且可以回复命令返回,但是我无法获得执行任何有用的命令。我试图让它插入一条记录,但我似乎无法连接到我的数据库 - 尽管我的实际应用程序工作正常 - 具有相同的连接细节。

我假设console.php配置文件不正确 - 但我不知所措。这是我的控制台配置文件

// This is the configuration for yiic console application.
// Any writable CConsoleApplication properties can be configured here.
return array(
    'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
    'name' => 'My Console Application',

    // preloading 'log' component
    'preload'=>array('log'),
    //import
    'import' => array(
        'application.models.*',
        'application.components.*',
        'application.extensions.*',
    ),
    // application components
    'components'=>array(
        'db' => array(
            'connectionString' => 'mysql:host=localhost;dbname=xxx',
            'emulatePrepare' => true,
            'username' => 'xxx',
            'password' => 'xxx',
            'charset' => 'utf8',
            'tablePrefix' => 'sys_',
            'class' => 'CDbConnection'
        ),
        'log'=>array(
            'class'=>'CLogRouter',
            'routes'=>array(
                array(
                    'class'=>'CFileLogRoute',
                    'levels'=>'error, warning',
                ),
            ),
        ),
    ),
    'params' => array(
        'keyword' => 'test',
    ),
);

我甚至无法让我的控制台命令写入日志,尽管我的实际应用程序写得很好。这是我的命令

class NewSysCommand extends CConsoleCommand {

    public function test() {
        echo "\nI've ran a sys test command successfully!\n\n";
        Yii::log('Command test to write to log', 'error');
    }

    public function captureIssueResponse(){
        Yii::log('captureIssueResponse write to log', 'warning');

        //Create our booking
        $issueResponse = new IssueResponses();
        $issueResponse->issue_id = 40;
        $issueResponse->ticket_id = "1111";
        $issueResponse->response = "new response from command line 1";
        $issueResponse->save();
    }

    public function run($args) {
        self::test();
        self::captureIssueResponse();
        return 0;
    }

}

这是我在使用' protected / yiic newSys'

在控制台中运行我的命令时遇到的错误
I've ran a sys test command successfully!

exception 'CDbException' with message 'CDbConnection failed to open the DB connection: SQLSTATE[HY000] [2002] No such file or directory' in /Applications/XAMPP/xamppfiles/htdocs/framework/db/CDbConnection.php:382
Stack trace:
#0 /Applications/XAMPP/xamppfiles/htdocs/framework/db/CDbConnection.php(330): CDbConnection->open()
#1 /Applications/XAMPP/xamppfiles/htdocs/framework/db/CDbConnection.php(308): CDbConnection->setActive(true)
#2 /Applications/XAMPP/xamppfiles/htdocs/framework/base/CModule.php(387): CDbConnection->init()
#3 /Applications/XAMPP/xamppfiles/htdocs/framework/base/CApplication.php(450): CModule->getComponent('db')
#4 /Applications/XAMPP/xamppfiles/htdocs/framework/db/ar/CActiveRecord.php(634): CApplication->getDb()
#5 /Applications/XAMPP/xamppfiles/htdocs/framework/db/ar/CActiveRecord.php(2361): CActiveRecord->getDbConnection()
#6 /Applications/XAMPP/xamppfiles/htdocs/framework/db/ar/CActiveRecord.php(411): CActiveRecordMetaData->__construct(Object(IssueResponses))
#7 /Applications/XAMPP/xamppfiles/htdocs/framework/db/ar/CActiveRecord.php(79): CActiveRecord->getMetaData()
#8 /Applications/XAMPP/xamppfiles/htdocs/MyApplication/sysadmin/protected/commands/NewSysCommand.php(23): CActiveRecord->__construct()
#9 /Applications/XAMPP/xamppfiles/htdocs/MyApplication/sysadmin/protected/commands/NewSysCommand.php(52): NewSysCommand->captureIssueResponse()
#10 /Applications/XAMPP/xamppfiles/htdocs/framework/console/CConsoleCommandRunner.php(71): NewSysCommand->run(Array)
#11 /Applications/XAMPP/xamppfiles/htdocs/framework/console/CConsoleApplication.php(92): CConsoleCommandRunner->run(Array)
#12 /Applications/XAMPP/xamppfiles/htdocs/framework/base/CApplication.php(180): CConsoleApplication->processRequest()
#13 /Applications/XAMPP/xamppfiles/htdocs/framework/yiic.php(33): CApplication->run()
#14 /Applications/XAMPP/xamppfiles/htdocs/MyApplication/sysadmin/protected/yiic.php(7): require_once('/Applications/X...')
#15 /Applications/XAMPP/xamppfiles/htdocs/MyApplication/sysadmin/protected/yiic(4): require_once('/Applications/X...')

谁能看到我做错了什么?

1 个答案:

答案 0 :(得分:2)

您应该将主机更改为127.0.0.1或尝试使用MySQL套接字,如下所示:

mysql:host=localhost;dbname=xxxx;unix_socket=/Applications/MAMP/tmp/mysql/mysql.sock

这适用于MAMP,但也可能适用于XAMPP。