【发布时间】:2017-01-25 12:49:10
【问题描述】:
我正在尝试在 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...')
谁能看到我做错了什么?
【问题讨论】:
-
配置看起来不错,您是否能够使用 PHPMyAdmin 或 HeidiSQL 等其他方法连接到数据库。可能是用户没有适当的权限
-
是的,PHPMyAdmin 不仅工作正常,而且我使用相同数据库凭据/权限的主应用程序也工作正常
-
如果您搜索确切的 mysql 错误:
SQLSTATE[HY000] [2002] No such file or directory,您会得到以下结果 stackoverflow.com/questions/2412009/…,所以您的 PHP 配置有问题 -
@TimvanderGaag - 好声音 - 答案是将 localhost 更改为 127.0.0.1!谢谢老哥