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);
Setup
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.
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
$connection->createCommand($query)->query();
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';
$post->save();
When I use this kind of code now, if fails on the database connect (so it does not look like incorrect tables or something)
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.
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.
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.
<?php
// 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(
'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
'name'=>'My app',
// preloading 'log' component
'preload'=>array('log'),
// autoloading model and component classes
'import'=>array(
'application.models.*',
'application.components.*',
),
// application components
'components'=>array(
'log'=>array(
'class'=>'CLogRouter',
'routes'=>array(
array(
'class'=>'CFileLogRoute',
'levels'=>'error, warning',
),
),
),
'user'=>array(
// enable cookie-based authentication
'allowAutoLogin'=>true,
),
'db'=>array(
array('components'=>
array('db'=>
array(
'class'=>'CDbConnection',
'connectionString'=>'mysql:host=phpmyadmin30.xxxx.com;dbname=xxx',
'username'=>'xxxx',
'password'=>'xxxx',
'charset'=>'utf8',
'emulatePrepare'=>true,
),
),
),
),
),
// application-level parameters that can be accessed
// using Yii::app()->params['paramName']
'params'=>array(
// this is used in contact page
'adminEmail'=>'webmaster@xxxx.nl',
),
);
console.php
<?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',
);