Step four :: Creating AR Model Class

Creating the project table
CREATE TABLE tbl_project
(
id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(128),
description TEXT,
create_time DATETIME,
create_user_id INTEGER,
update_time DATETIME,
update_user_id INTEGER
);

Copy and paste the above schema in the database. You will have tbl_project table create. 

Creating the AR model class

Now that we have the tbl_project table created, we need to create the Yii model class to allow us to easily manage the data in that table. We introduced Yii's Object -relational Mapping (ORM) layer and Active Record (AR). As of version 1.1.2 of Yii, there is a new and more sophisticated interface available called Gii. Gii is a highly customizable and extensible web-based code generation platform that takes the yiic shell command to new heights. We will be using this new platform to create our
new model class.

Using Gii to create our Project AR class

Go ahead and enter the password you provided during the configuration. A successful entry will take you to the following main menu page of Gii:

As we want to create a new model class for our tbl_project table, the Model Generator option seems like the right choice. Clicking that link takes us to the following page:


The Table Prefix field is primarily used to help Gii determine how to name the AR class we are generating. If you are using a prefix, you can add this here. This way, it won't use that prefix when naming the new class. In our case, we are using the tbl_ prefix, which also just happens to be what this form field defaults to. So, specifying this value will mean that our newly generated AR class will be named Project, rather than tbl_project.

The next two fields are asking for our Table Name and the name of the class file we want it to generate. Type in the name of our table, tbl_project, and watch as the model class name auto-populates. The convention for the Model Class name is the name of the table, minus the prefix, and starting with an uppercase letter.So, it will assume a name of Project for our Model Class name, but of course you can customize this.
The next few fields allow for further customization. The Base Class field is used to specify the class from which our Model Class will extend. This will have to be CActiveRecord or a child class thereof. The Model Path field allows you to specify where in the application folder structure to output the new file. The default is protected/models/ (also known as application.models). The last field allows us to specify a template on which the generated code is based. We can customize the default one to meet any specific needs we have that might be common to all such  class files. For now, the default values for these fields meet our needs just fine.
Proceed by clicking on the Preview button. This will result in the following table
that is displayed at the bottom of the page:


This link allows you to preview the code that will be generated. Before you hit Generate, click on the models/Project.php link. A pop up will open showing the code that will be generated. Okay, close this popup and go ahead and click on the Generate button. Assuming all went well, you should see the following screenshot displayed at the bottom of the page:


Gii has created for us a new Yii AR model class, named (as we instructed it to) as Project.php, and placed it (as we instructed it to) in the default Yii location for model classes, protected/models/. This class is a wrapper class for our tbl_project database table. All of the columns in the tbl_project table are accessible as properties of the Project AR class.

No comments:

Post a Comment