Console Application - Cdbconnection Failed

Hi everyone,

I come from italian forum, where I’ve already asked about this issue, but I have not received answers. I hope someone of you can help me with it.

I’m trying to set up a console application. I’ve followed this, and a simple example like this




<?php


class TestCommand extends CConsoleCommand

{

    public function run($args) {

        echo "hello";

    }

}


?>



is working fine. But when i try to load a model, like this




<?php


class TestCommand extends CConsoleCommand

{

    public function run($args) {

    	$users = User::model()->findAll();

    	foreach ($users as $user) {

    	     echo $user->first_name;

    	}

    }

}


?>



I get




CDbConnection failed to open the DB connection: could not find driver



this is the config file




<?php


return array(

	'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',

	'name'=>'Myapp',

	'import'=>array(

		'application.models.*',

		'application.components.*',

	),

	'components'=>array(	 

		'db'=>array(

			'class' => 'CDbConnection',

			'connectionString' => 'mysql:host=localhost;dbname=myapp',

			'emulatePrepare' => true,

			'username' => 'root',

			'password' => 'root',

			'charset' => 'utf8',

		),

	)

);


?>



The config db array is the same as the main.php inside config folder, so can’t be wrong.

What’s missing?

Thank you very much.

Giacomo

Are you on Debian or Ubuntu? I suspect the mysql PDO driver isn’t registered for your PHP cli.

Edit - for reference: http://www.yiiframework.com/doc/guide/1.1/en/quickstart.first-app-yiic (second note)

Hi, thanks for your quick answer.

Right now I’m working on local machine with windows, just running the command from the prompt.

Btw, the PDO is installed, the webApplication use the same configuration and works fine. Do I’ve to move to real server to test it?

thanks

Edit: I’ve moved the proj on server, and it’s working fine :) . Thanks for your help

Hi there,

I seem to be lost in the same territory of CDbConnection failing in a console application. And just like the situation of Giacomo, the same configuration and code works correctly in the web application.

But unlike Giacomo, I’m getting a different error




[error] [exception.CDbException] exception 'CDbException' with message 'CDbConnection failed to open the DB connection: SQLSTATE[HY000] [2002] No such file or directory' in /Users/pdatar/yii/framework/db/CDbConnection.php:382



I’ve explained my situation in another forum thread here

http://www.yiiframework.com/forum/index.php/topic/52069-getting-a-database-error-while-running-console-commands/page__p__241122__fromsearch__1#entry241122

Thank again in advance for helping.

I want to be able to test the configuration on my local machine first before moving it to a live production server. My set up is:

MySQL and PHP from MAMP

Running on Mac OS X 10.9

And it’s using version 5 of php