【发布时间】:2016-02-17 00:52:33
【问题描述】:
我正在尝试使用 yii 创建一个简单的 Web 应用程序。我已经安装了 WAMP 2.5 和 yii 1.1.x。我还创建了一个名为 yiitest 的框架应用程序。我还使用 mysql 创建了一个名为 yiitest 的数据库,该数据库有一个名为 people 的表,并具有以下列:pid、fname、lname、dob、zip。我正在尝试使用 gii 构建 Web 应用程序的基本功能。我可以登录 gii 并导航到 Controller Generator Form Generator 和 Module Generator,但是当我尝试单击 Crud Generator 或 Model Generator 时,我收到了 CDbException 错误。
这是我的代码:
受保护的/config/database.php
<?php
// This is the database connection configuration.
return array(
//'connectionString' => 'sqlite:'.dirname(__FILE__).'/../data/testdrive.db',
// uncomment the following lines to use a MySQL database
'connectionString' => 'mysql:host=localhost;dbname=yiitest',
'emulatePrepare' => true,
'username' => 'yiiUser',
'password' => 'p4ssw0rd',
'charset' => 'utf8',
);
受保护的/config/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 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'=>'password',
// 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>',
),
),
// database settings are configured in database.php
'db'=>require(dirname(__FILE__).'/database.php'),
'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',
),
);
任何关于让 gii 的模型生成器和 Crud Generator 工作的帮助都会非常有帮助!
****编辑**** 这是我收到的错误图片:
【问题讨论】:
-
CdbException 错误的详细信息是什么?
-
CDbException 下的 grat 文本区域中没有任何内容,但至于行错误,这是突出显示的内容:C:\wamp\www\yii\framework\gii\generators\model\ModelCode.php (60) 58 public function init() 59 { 60 if(Yii::app()->{$this->connectionId}===null) 61 throw new CHttpException(500,'需要有效的数据库连接才能运行这个生成器。'); 62 $this->tablePrefix=Yii::app()->{$this->connectionId}->tablePrefix; 63 父::init(); 64}
-
@user908759 尝试在
database.php文件中使用127.0.0.1而不是localhost。 -
请显示您的
/database.php内容.. -
感谢大家的回复@user908759 添加 127.0.0.1 而不是 localhost 导致同样的错误。