Pages

Tuesday, December 10, 2013

Test your Database Connection

After setting up the your MySQL connection in your web application (see my previous post), you should ensure that your database connection is actually working especially for new server setups. Let me introduce you to the interactive shell for your application and ensure that there are no errors when you attempt to reference the application db component.

Using the interactive shell

As you would remember, we used the command line utility called yiic to create a new web application in Yii. Another command you can use with yiic is called shell. This will allow you to run PHP commands within the context of the Yii application, straight from the command line.

To start the shell, go to the root directory of your application, that is the directory where you can located index.php entry script. Then run the yiic utility and passing in shell as the command. See the screenshot below





This will allow you to enter the commands directly after the >> prompt.

You would want to test your db connection to ensure that your MySQL Database is accessible from your web application. You can simply echo out the connection string and verify that it returns what you have set in the MySQL configuration (see Connect to a MySQL Database). So from the shell prompt, just type in:
>> echo Yii::app()->db->connectionString; 

It should return something similar to the following:
mysql:host=localhost;dbname=<db-name>

This will show that the db application component is configured correctly and available for use in your web applicaiton.

*Note: To exit from shell, just type exit from the prompt.

Monday, December 9, 2013

Connect to a MySQL Database in Yii

Configuring MySQL Database in Yii

Before we continue the discussion with the other features in Gii, let me show you how to configure your Yii Web Application to connect to a MySQL database. Yii's Data Access Objects (DAO) was built on top of the PHP Data Objects (PDO) extension. All the supported database management systems (DBMS) are encapsulated behind a single uniform interface. This means that the code can remain database independent and the application developed using Yii DAO can be easily switched from one DBMS to another without the need for modification.

Let us assume that we are using MySQL Database. You must open the /protected/config/main.php file and uncomment the MySQL script below:

'db'=>array(
            'connectionString' => 'mysql:host=localhost;dbname=<database-name.',
            'emulatePrepare' => true,
            'username' => '[your-db-username]',
            'password' => '[your-db-password]',
            'charset' => 'utf8',
        ),

Also, do not forget to comment the SQL connection script that was configured once the web application was created via yiic. See the script below
/*
'db'=>array(
            'connectionString' => 'sqlite:'.dirname(__FILE__).'/../data/testdrive.db',
*/

Gii: Controller Generator

Welcome to the Yii Code Generator

With Gii configured and enabled, you may access Gii by navigating via http://localhost/<name_of_application>/index.php?r=gii. After successful entry of your password (unless you specified that a password should not be used, see Enabling Gii), you are presented with a menu page listing Gii's main features.


Controller GeneratorYou can try to create a new controller, so click on the Controller Generator menu item. This will bring you to a form that allows you to fill out the relevant details to create a new Yii controller class. You must fill out the Controller ID value and add an action ID value. Then click on the preview button, this will show you the files to be generated once you click on the generate button. See the screenshot below:


In addition to the controller class, Gii is also going to create a view file for each of the Action IDs that you have specified. You should notice that if message is the Controller ID then the corresponding class file will be named MessageController.php. Similarly, if you provide an Action ID value of hello, you can expect to have a method name in the controller called actionHello.

We will tackle more details about actions and controllers in future posts.