1

I'm trying to set up a console command in Yii, and can echo out a return to the command fine, but i can't get the command to do anything useful. Im trying to get it to insert a record, but i cannot seem to connect to my database - despite the fact my actual application works perfectly - with the same connect details.

I'm assuming the console.php config file is incorrect - but am at a loss. Here's my console config file

// 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',
    ),
);

I can't even get my console command to write to the log, again, despite my actual application writes fine. here my command

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;
    }

}

and this is the error i get when trying to run my command in console using '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...')

Can anyone see what i'm doing wrong?

grhmstwrt
  • 341
  • 1
  • 6
  • 20
  • the config looks fine, Are you able to connect to the DB using another method like PHPMyAdmin, or HeidiSQL. it could be that the user does'nt have the proper privileges – Tim Jan 25 '17 at 13:26
  • Yes, not only does PHPMyAdmin work fine, but my main application which used the same database credentials / privledges works fine too – grhmstwrt Jan 25 '17 at 13:29
  • If you search on the exact mysql Error : ```SQLSTATE[HY000] [2002] No such file or directory``` you get the following result http://stackoverflow.com/questions/2412009/starting-with-zend-tutorial-zend-db-adapter-throws-exception-sqlstatehy000 so properly there is something wrong with your PHP config – Tim Jan 25 '17 at 13:30
  • @TimvanderGaag - good shout - the answer was to change localhost to 127.0.0.1! Thanks mate – grhmstwrt Jan 25 '17 at 14:23

1 Answers1

2

You should change the host to 127.0.0.1 or try to use MySQL socket like so:

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

This is working well for MAMP, but possibly for XAMPP as well.

wmkamerman
  • 21
  • 2