2

I am trying to create a simple web application using yii. I have install WAMP 2.5 and yii 1.1.x. I have also created a skeleton application called yiitest. I have also created a database with mysql called yiitest which has a table called persons and has the following columns: pid, fname, lname, dob, zip. I am trying to build the basic functionality of the web app using gii. I can log into gii and navigate to Controller Generator Form Generator and Module Generator, but when I try to click on Crud Generator or Model Generator I get a CDbException error.

Here is my code:

protected/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',

);

protected/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',
        ),
);

Any help on getting gii's Model Generator and Crud Generator working would be very helpful!

****Edit**** Here is a picture of the error I receive:

CDbException Error

user908759
  • 1,355
  • 8
  • 26
  • 48
  • What are the details of your CdbException error? – Joey Nov 16 '15 at 01:28
  • Nothing is in the grat text area under CDbException, but as for the line error this is what is highlighted: 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,'A valid database connection is required to run this generator.'); 62 $this->tablePrefix=Yii::app()->{$this->connectionId}->tablePrefix; 63 parent::init(); 64 } – user908759 Nov 16 '15 at 02:53
  • @user908759 try using `127.0.0.1` instead of `localhost` in `database.php` file. – Criesto Nov 16 '15 at 06:08
  • show your `/database.php` content please.. – ScaisEdge Nov 16 '15 at 08:20
  • Thank you everyone for your responses @user908759 adding 127.0.0.1 instead of localhost resulted in the same error. – user908759 Nov 16 '15 at 15:18
  • @scaisEdge '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', ); – user908759 Nov 16 '15 at 15:19

1 Answers1

0

Could be you don'have a valid db connection

check in you dirname(__FILE__).'/database.php' if you have valid parameter

somethings like this

    'db'=>array(
        'connectionString' => 'mysql:host=localhost;dbname=my_dbname',
        'emulatePrepare' => true,
        'username' => 'root',
        'password' => 'my_pwd',
        'charset' => 'utf8',
    ),
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107