Yii 1.1: MVC primer and 5 minute form walkthrough

7 followers

This tutorial assumes some basic knowledge of Yii and a functional development environment.

Before continuing please ensure:

  • You have created a yii webapp
  • You have connected your web app to a working database via configuration of protected/main.php file
  • You have configured Gii for use (also in protected/main.php)
  • You are using path based url's and using .htaccess to filter 'index.php' from your url's. This isn't strictly necessary but it will definitely make the tutorial easier to follow

Step 1: Create a database table

It's really useful to have a central repository of all the database schema which you are using, so go to the location below and add your sql statements to the appropriate file. In my case it's:

protected/data/schema.mysql.sql 

Note, if you are using a prefix for your table (such as tbl_) then you'll need to have the following option in the database config section of /protected/config/main.php

'tablePrefix' => 'tbl_',

At this juncture it's definitely worth thinking about your table structure as you can save yourself a lot of time later on. For instance if 'title', 'first' and 'email' are actually going to be required fields on your form, you'll save time by making them 'not null'.

When you come to create the form Yii will automatically create validation rules based on your table structure - so in the case below Yii won't validate a null title or a title which is more than 5 characters.

CREATE TABLE tbl_details (
    uniq VARCHAR(20) NOT NULL PRIMARY KEY,
    date_in date DEFAULT NULL, 
    time_in time DEFAULT NULL, 
    title VARCHAR(5) NOT NULL,
    first VARCHAR(30) DEFAULT NULL,
    last VARCHAR(30) DEFAULT NULL,
    email VARCHAR(60) DEFAULT NULL
);

Now go ahead and create the table (you can do this anyway you like, I like to use Sequel Pro so simply paste the above into the 'query' tab).

Step 2: Create a model

To access the data in our table we're going to need a model. Models can be understood as representations of data. Whilst you can easily create a model by simply going to the protected/models and writing one, it's much easier to use Gii.

If you have path based (clean) urls enabled and are filtering out index.php from your path you will find Gii here:

http://hostname/yii_app_folder/gii

Otherwise, Gii will be located at:

http://hostname/path/to/index.php?r=gii

Go to the Model Generator and enter the information as required. Now go ahead and generate the code.

Note, if Gii is unable to create the model it might be because your webserver process does not have the necessary permissions to write to the models folder.

It's worth noting here that you might want to modify the attributeLabels() method in your model. Otherwise, when you come to create your form it will use the defaults listed here which are simply the tidied-up names of your database fields.

Step 3: Create a controller

Now we have a model we need to create a controller. You can think of a controller as mediating between a model and a view.

Go to the 'Controller Generator' link and enter the name of your controller. Remember that if your controller is called 'details' then you will access it's methods at the following url (that's if you have path based url's and are filtering out index.php):

http://hostname/yii_app_folder/details

You'll notice that Gii will also ask if you would like to create a view for your controller. The view will be found here:

/protected/views/your_controller_name/index.php

Go ahead and create the view as well. We're not actually going to be using this view but it's a good idea to create it anyway as part of the learning process.

Now go to the url below:

http://hostname/yii_app_folder/your_controller_name

If you have logging enabled you can see everything that is happening behind the scenes. For now, note the following.

  • The actionIndex() of your controller is called
  • Your controller is using protected/views/layouts/column2.php to render your view (protected/views/your_controller/index.php)

Step 4: Create a view

Return to Gii and click on the 'Form Generator' link.

Fill in the details as required making sure the path to your view is such:

application.views.your_controller_name

Now we've constructed the basics let's recap what we have thus far.

/protected/models/Details
/protected/controllers/DetailsController
/protected/views/details/index.php
/protected/views/details/details_form.php

Step 5: View the form

Instead of rendering index.php, we might just want to view the form directly. Edit the following method in your DetailsController:

/* 
* Lists all models.
*/
public function actionIndex()
{   
    $model=new Details;

    if(isset($_POST['Details']))
    {   
        $model->attributes=$_POST['Details'];

        if ($model->save())
        {   
            print "Your model has been saved";
            die();  
        }   
    }   
    $this->render('details_form',array(
            'model'=>$model,
        )); 
}   

Visit the url below to view your form.

http://hostname/yii_app_folder/your_controller_name

Be the first person to leave a comment

Please to leave your comment.

Write new article
  • Written by: rix.rix.
  • Category: Tutorials
  • Yii Version: 1.1
  • Votes: +8
  • Viewed: 15,577 times
  • Created on: May 11, 2012
  • Last updated: Jun 21, 2012
  • Tags: Forms, tutorial, mvc