Gii, Model Generator Is Not Working

Model Generator is not working. What i should change in the code or configuration?

I have created in PhpAdmin simple table ‘tbl_demo’.

There is db connection added as an application component,

i mean that i have uncommented MySQL lines in protected/config/main.php.

Apache and MySQL are running in XAMPP.

When i go to localhost/demo_project/index.php?r=gii/default/index

i see the page with several options like:

Controller Generator


Crud Generator


Form Generator


Model Generator


Module Generator

When i try to use Model Generator i am getting the errors:


CDbConnection failed to open the DB connection: SQLSTATE[HY000] [1045] Access denied for user 'amber'@'localhost' (using password: YES) 





C:\xampp\htdocs\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:\xampp\htdocs\yii\framework\db\CDbConnection.php(330): CDbConnection->open()

#1 	

+

 C:\xampp\htdocs\yii\framework\db\CDbConnection.php(308): CDbConnection->setActive(true)

#2 	

+

 C:\xampp\htdocs\yii\framework\base\CModule.php(387): CDbConnection->init()

#3 	

+

 C:\xampp\htdocs\yii\framework\base\CModule.php(103): CModule->getComponent("db")

#4 	

+

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

#5 	

+

 C:\xampp\htdocs\yii\framework\web\CFormModel.php(39): ModelCode->init()

#6 	

+

 C:\xampp\htdocs\yii\framework\gii\CCodeGenerator.php(153): CFormModel->__construct()

#7 	

+

 C:\xampp\htdocs\yii\framework\gii\CCodeGenerator.php(63): CCodeGenerator->prepare()

#8 	

+

 C:\xampp\htdocs\yii\framework\web\actions\CInlineAction.php(49): CCodeGenerator->actionIndex()

#9 	

+

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

#10 	

+

 C:\xampp\htdocs\yii\framework\web\CController.php(286): CController->runAction(CInlineAction)

#11 	

+

 C:\xampp\htdocs\yii\framework\web\CController.php(265): CController->runActionWithFilters(CInlineAction, array())

#12 	

+

 C:\xampp\htdocs\yii\framework\web\CWebApplication.php(282): CController->run("index")

#13 	

+

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

#14 	

+

 C:\xampp\htdocs\yii\framework\base\CApplication.php(180): CWebApplication->processRequest()

#15 	

–

 C:\xampp\htdocs\trackstar\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();

I am sorry for the question.

The reason was simple. I have to use the correct username in project/protected/config/main.php

In my case it is ‘root’, not ‘amber’.

	'db'=>array(


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


		'emulatePrepare' => true,


		'username' => 'root',


		'password' => '',


		'charset' => 'utf8',


	), ...