Tuesday, November 8, 2011

Configuration Yii Framework to MySQL Database

By default Yii Framework to connect to SQLite database. It can take a look at webappname \ protected \ config \ main.php. Consider the following code:
*/
'db'=>array(
 'connectionString' => 'sqlite:'.dirname(__FILE__).'/../data/testdrive.db',
),
// uncomment the following to use a MySQL database
/*
'db'=>array(
 'connectionString' => 'mysql:host=localhost;dbname=testdrive',
 'emulatePrepare' => true,
 'username' => 'root',
 'password' => '',
 'charset' => 'utf8',
),
*/

In the code above shows the application to connect to SQLite database where there are no tag "comment" while in MySQL there is a tag "comment" which means the code will not be processed. We need to do is

  1. Adding a tag comment / * and * / on SQLite SQLite database that is not executed. Consider the following code:
    /*
    'db'=>array(
     'connectionString' => 'sqlite:'.dirname(__FILE__).'/../data/testdrive.db',
    ),
    */
  2. Remove the tag comment / * and * / to MySQL database and rename testdrive.db (default) with the name of your database. example yiicode_db
    // Using MySQL database
    'db'=>array(
     'connectionString' => 'mysql:host=localhost;dbname=yiicode_db',
     'emulatePrepare' => true,
     'username' => 'root',
     'password' => '',
     'charset' => 'utf8',
    ),
So the whole database connection code would look something like the following:
/*
'db'=>array(
 'connectionString' => 'sqlite:'.dirname(__FILE__).'/../data/testdrive.db',
),
*/
// Using MySQL database
'db'=>array(
 'connectionString' => 'mysql:host=localhost;dbname=yiicode_db',
 'emulatePrepare' => true,
 'username' => 'root',
 'password' => '',
 'charset' => 'utf8',
),
Yii Framework Tutorials

Tuesday, November 1, 2011

Installing Yii Framework in XAMPP

Here I will discuss the details of the installation steps Yii Framework. But before, there are some you need to prepare to install Yii:
  • Yii Framework - Download at http://www.yiiframework.com/download/
  • Web and database Server, here I am using XAMPP - Download at http://www.apachefriends.org/en/xampp.html
  • Web Browser, here I use Mozilla Firefox - Download at http://www.mozilla.org/id/firefox/new/
After you download all, let's start installing yii. Here, I use windows 7 as the operating system.

1. Install XAMPP, here I did not explain how to install it, but just to be safe when the computer error and have to reinstall, it should be installed on a drive other than C, such as drive D, E, or others. Here I installed on drive D

2. Extract yii-x.x.x.xxxxx.tar.gz that you download, then change its name to yii, and to be more easily placed in the folder XAMPP/htdocs, so that such XAMPP/htdocs/yii.

3. Open the comand line by pressing ctrl + r and type cmd

Installing Yii Framework in Xampp
Yii Web Application
Microsoft Windows [Version 6.1.7600]
Copyright 2009 Microsoft Corporation. All rights reserved
C: \ Users \ santje>

4. Typed in the comand-line d:

Microsoft Windows [Version 6.1.7600]
Copyright 2009 Microsoft Corporation. All rights reserved
C: \ Users \ santje> d:
D: \>

5. Typing again cd xampp

Microsoft Windows [Version 6.1.7600]
Copyright 2009 Microsoft Corporation. All rights reserved
C: \ Users \ santje> d:
D: \> cd xampp
D: \ xampp>

6. After that typing cd php

Microsoft Windows [Version 6.1.7600]
Copyright 2009 Microsoft Corporation. All rights reserved
C: \ Users \ santje> d:
D: \> cd xampp
D: \ xampp> cd php
D: \ xampp \ php>

7. Typing again php.exe D:\xampp\htdocs\yii\framework\yiic webapp D:\xampp\htdocs\yiicode
Description: D: \ xampp \ htdocs \ yiicode means the application will be installed on xampp - htdocs, while yiicode is the name of my application to be made

Microsoft Windows [Version 6.1.7600]
Copyright 2009 Microsoft Corporation. All rights reserved
C: \ Users \ santje> d:
D: \> cd xampp
D: \ xampp> cd php
D: \ xampp \ php>
D: \ xampp \ php> php.exe D:\xampp\yii\ framework\yiic\webapp D:\xampp\htdocs\yiicode
Create a Web application under 'D: \ xampp \ htdocs \ yiicode'? [yes: no]

8. The last typed yes and enter.
Your application has been succesfully created under D: \ xampp \ htdocs \ yiicode

9. Open xampp-control.exe click the start button on the Apache and MySQL, and then open your web browser and type localhost/yiicode. Congratulations you have successfully created your first application yii

Typical Workflow of a Yii Application

Before we go any further, let us know how to work the first Yii application when handling a user request.

Here is a typical workflow of a series of Yii application when handling user requests.
  1. User makes a request to the URL http://www.yiicode.com/index.php?r=post/show&id=1 and Web server handles the request by running the bootstrap script index.php.
  2. Bootstrap script creates an instance application and run it.
  3. Applications to get detailed information from the user requests an application component named request.
  4. The application determines the requested controller and action with the help of an application component named urlManager. In this example, the controller is a post that refers to class PostController; and actions show the true meaning is determined by the controller.
  5. Application creates an instance controller is required to further handle user requests. Show the controller determines that the action refers to a method named actionShow the controller class. Then create and run a filter (eg access control, measurement) associated with this action. The action is executed if allowed by the filter.
  6. Action to read the Post Model where the ID is 1 from the database.
  7. Action renders a view named show with the model of the Post.
  8. View to read and display the attributes of the Post model.
  9. View running multiple widgets.