Db Connection Error When Following Blog Tutorial

Hey,

I am trying to learn the basics of the Yii framework so I started with the blog tutorial.

This one: http://www.yiiframework.com/doc/blog/1.1/en

Only when I got to ‘Scaffolding’ I got a DB error when opening the model generator with the Yii code generator.

The error I got:


CDbException


CDbConnection.connectionString cannot be empty.


C:\wamp\www\yii\framework\gii\generators\model\ModelCode.php(60)


48         ));

49     }

50 

51     public function requiredTemplates()

52     {

53         return array(

54             'model.php',

55         );

56     }

57 

58     public function init()

59     {

60         if(Yii::app()->{$this->connectionId}===null)

61             throw new CHttpException(500,'A valid database connection is required to run this generator.');

62         $this->tablePrefix=Yii::app()->{$this->connectionId}->tablePrefix;

63         parent::init();

64     }

65 

66     public function prepare()

67     {

68         if(($pos=strrpos($this->tableName,'.'))!==false)

69         {

70             $schema=substr($this->tableName,0,$pos);

71             $tableName=substr($this->tableName,$pos+1);

72         }

Stack Trace

#0	

+  C:\wamp\www\yii\framework\db\CDbConnection.php(330): CDbConnection->open()

#1	

+  C:\wamp\www\yii\framework\db\CDbConnection.php(308): CDbConnection->setActive(true)

#2	

+  C:\wamp\www\yii\framework\base\CModule.php(387): CDbConnection->init()

#3	

+  C:\wamp\www\yii\framework\base\CModule.php(103): CModule->getComponent("db")

#4	

+  C:\wamp\www\yii\framework\gii\generators\model\ModelCode.php(60): CModule->__get("db")

#5	

+  C:\wamp\www\yii\framework\web\CFormModel.php(39): ModelCode->init()

#6	

+  C:\wamp\www\yii\framework\gii\CCodeGenerator.php(153): CFormModel->__construct()

#7	

+  C:\wamp\www\yii\framework\gii\CCodeGenerator.php(63): CCodeGenerator->prepare()

#8	

+  C:\wamp\www\yii\framework\web\actions\CInlineAction.php(49): CCodeGenerator->actionIndex()

#9	

+  C:\wamp\www\yii\framework\web\CController.php(308): CInlineAction->runWithParams(array("r" => "gii/model/index"))

#10	

+  C:\wamp\www\yii\framework\web\CController.php(286): CController->runAction(CInlineAction)

#11	

+  C:\wamp\www\yii\framework\web\CController.php(265): CController->runActionWithFilters(CInlineAction, array())

#12	

+  C:\wamp\www\yii\framework\web\CWebApplication.php(282): CController->run("index")

#13	

+  C:\wamp\www\yii\framework\web\CWebApplication.php(141): CWebApplication->runController("gii/model/index")

#14	

+  C:\wamp\www\yii\framework\base\CApplication.php(180): CWebApplication->processRequest()

#15	

–  C:\wamp\www\myfirstsite\index.php(13): CApplication->run()

08 defined('YII_DEBUG') or define('YII_DEBUG',true);

09 // specify how many levels of call stack should be shown in each log message

10 defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);

11 

12 require_once($yii);

13 Yii::createWebApplication($config)->run();

14 

15 

16 return array(

17     'import'=>array(

18         'application.models.*',

I saw an error like this before and it was solved by uncommenting the db connection but mine already is uncommented.

I’m using wamp btw.

My main config file :


<?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(

    'import'=>array(

        'application.models.*',

        'application.components.*',

    ),

    

    'modules'=>array(

        'gii'=>array(

            'class'=>'system.gii.GiiModule',

            'password'=>'wq5z9wf2h109050',

        )

    )

);


return array(

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

	'name'=>'My Web Application',


	// preloading 'log' component

	'preload'=>array('log'),


	// autoloading model and component classes

	'import'=>array(

		'application.models.*',

		'application.components.*',

	),


	'modules'=>array(

		// uncomment the following to enable the Gii tool

		/*

		'gii'=>array(

			'class'=>'system.gii.GiiModule',

			'password'=>'Enter Your Password Here',

			// If removed, Gii defaults to localhost only. Edit carefully to taste.

			'ipFilters'=>array('127.0.0.1','::1'),

		),

		*/

	),


	// application components

	'components'=>array(

		'user'=>array(

			// enable cookie-based authentication

			'allowAutoLogin'=>true,

		),

		// uncomment the following to enable URLs in path-format

		/*

		'urlManager'=>array(

			'urlFormat'=>'path',

			'rules'=>array(

				'<controller:\w+>/<id:\d+>'=>'<controller>/view',

				'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',

				'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',

			),

		),

		*/

		'db'=>array(

			'connectionString' => 'sqlite:/www/myfirstsite/protected/data/blog.db',

			'tablePrefix'=>'tbl_',

		),

		// uncomment the following to use a MySQL database

		/*

		'db'=>array(

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

			'emulatePrepare' => true,

			'username' => 'root',

			'password' => '',

			'charset' => 'utf8',

		),

		*/

		'errorHandler'=>array(

			// use 'site/error' action to display errors

			'errorAction'=>'site/error',

		),

		'log'=>array(

			'class'=>'CLogRouter',

			'routes'=>array(

				array(

					'class'=>'CFileLogRoute',

					'levels'=>'error, warning',

				),

				// uncomment the following to show log messages on web pages

				/*

				array(

					'class'=>'CWebLogRoute',

				),

				*/

			),

		),

	),


	// application-level parameters that can be accessed

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

	'params'=>array(

		// this is used in contact page

		'adminEmail'=>'webmaster@example.com',

	),

);

Anyone got an idea?

Hi and welcome to the Yii forum

If you are just starting with Yii then start first with the Definitive Guide to Yii to get all the basics - http://www.yiiframework.com/doc/guide/

In your case you did not configure the database connection - http://www.yiiframew…ing-to-database

[color="#006400"]NOTE: moved to proper section General discussion instead of tips, tutorials[/color]

I did that in my main file. As you can see I changed it for my blog.db file. I’m not using MySQL. I don’t see anything in the text you sent me that that I have’nt already done. Do I need to delete the table prefix? I don’t think that’s it because they use it in the tutorial.

Ops, I did not even notice you posted the config file…

Check again your config file you have something like this:




return array(

   ...

)


return array(

   ...

)



only the first array is returned and there is no connection string defined ;)

You have two returns in config, that’s why main config is never reached.

UPD: oops, ninja’d by Maurizio.

Oh okay, but the tutorial told me to add it in the main file. Do I delete it and uncomment the Gii part? Where it says uncomment the following to enable the Gii tool.

basically you just need to remove first array and in second replace this


                // uncomment the following to enable the Gii tool

                /*

                'gii'=>array(

                        'class'=>'system.gii.GiiModule',

                        'password'=>'Enter Your Password Here',

                        // If removed, Gii defaults to localhost only. Edit carefully to taste.

                        'ipFilters'=>array('127.0.0.1','::1'),

                ),

                */

by your own code:


        'gii'=>array(

            'class'=>'system.gii.GiiModule',

            'password'=>'wq5z9wf2h109050',

        )