[SOLVED] Mysql "Property 'CDbConnection.0' is not defined."


After doing some research under diff php framewords (ci/symphony/zend) I came to the conclusion I wanted to try YII. So far all looks and goes verry well and the framework helped me writing less and better code pretty well!

I regret that my first post on this forum has to be a question :)

After some searching (this site/forum/docs/google) I could not find to answer to my problem (see topic title and info below);


Xampp-lite windows edition with php5.2.9 and yii-1.0.7.r1212.

Mysql database is running on a shared webhost.

What I want :)

I want to auto init the db connection via the main.php located in the config folder.

In the application i want to reference that connection via Yii::app()->db to do some queries.

main.php db lines











SiteController.php code

$dbActions->conn = Yii::app()->db;

$dbActions->sql = "select id";

$dbActions->command = $conn->createCommand($dbActions->sql);

$dbActions->rows = $dbActions->command->queryAll();

the above code will result in the following error;



Property "CDbConnection.0" is not defined.

Source File


what am I missing or doing wrong? thanks in advance!

You may want to deepen your knowledge regarding Yii’s capabilites to access databases.

You can’t modify Yii::app()->db->sql, as ‘db’ only refers to an application component.

Welcome to the forums!


If you only want to run pure sql instead of using ActiveRecord, then you probably should use

Yii::app()->db->createCommand(‘select id from table’)->query();

pestaa, thanks for your quick reply.

I do want to use Database Access Objects and/or Active record. To start using that I created the code in the first post to make a connection but that does not work. When that works I can continue.

So; Im still looking for a way to (auto) connect to the database.

I am using the ‘workaround’;

$connection = new CDbConnection('mysql:host=phpmyadmin30.xxxx.com;dbname=xxxx','xxxx','xxxx');

$connection->active = true;

This works and I can query the database fine using


But I would like to use the Yii::app()->db functions like Yii::app()->db->quoteValue and the Active Record stuff:

$post=new Post;

$post->title='sample post';

$post->content='post body content';


When I use this kind of code now, if fails on the database connect (so it does not look like incorrect tables or something)

I succeeded using this code before retrieving data from AR models:




In you main.php configuration file, you can set autoConnect property to a boolean, indicating whether you want to automatically create a connection to the database.

Since its default value is true, you don’t have to do anything in order to initiate db connection.

Is this what you wanted?

Tommy thanks for your reply. When I use your code in my SiteController I get the same error that

Property "CDbConnection.0" is not defined.

It does not matter if I put that code in one of the models/views or other controllers. So that did not solve my problem

Pestaa; I have read that indeed the auto connect feature is true default. So i should be having a database connection (and not the CDbConnection.0 error).

In the meantime I have tried to use an ip instead of the webadres of the sql server, this did not solve the CDbConnection.0 error.

I also succeeded by defining a second component "db2", using it for the DAO calls only. But in my case, I could use the standard "db" connection if I closed it before attempting AR accesses. Perhaps in your case - if you use AR first - you may need to close and reopen the connection? Not saying this is the proper way to handle a mix of DAO and AR calls, just what I found out experimenting.

thanks for your reply tri,

the DOA functions work ok because I defined the connection ‘manually’ in the script. I also defined the database connection in the main.php config file but the framework isnt using this info.

I cant use AR commands but do want to try them. my script does alot/only insert and delete queries, nothing fancy. But when using the AR commands I get the error that there is no db connection though I specified it in the main.php.

Could you show your complete app config?



// uncomment the following to define a path alias

// Yii::setPathOfAlias('local','path/to/local-folder');

// This is the main Web application configuration. Any writable

// CWebApplication properties can be configured here.

return array(


	'name'=>'My app',

	// preloading 'log' component


	// autoloading model and component classes





	// application components







					'levels'=>'error, warning',





			// enable cookie-based authentication



















	// application-level parameters that can be accessed

	// using Yii::app()->params['paramName']


		// this is used in contact page






// This is the configuration for yiic console application.

// Any writable CConsoleApplication properties can be configured here.

return array(


	'name'=>'My Console Application',


You config is incorrect: ‘components’=>‘db’=>‘components’=>‘db’

qiang, thnx!

it was that easy! sorry for not have seeing it myself.

all db actions I tested so far work now!

changed the topic title to solved.