Scaffolding

Create, read, update and delete (CRUD) are the four basic operations of persistent storage. In our blog application, the major task is to implement the CRUD operations for both posts and comments. In this section, we will use the yiic tool to accomplish this task. This process is also known as scaffolding.

Open a command window and run the following commands:

% /wwwroot/yii/framework/yiic shell /wwwroot/blog/index.php
Yii Interactive Tool v1.0
Please type 'help' for help. Type 'exit' to quit.
>> model User
......
>> model Post
......
>> model Tag
......
>> model Comment
......
>> crud Post
......
>> crud Comment
......
>> exit

Info: Some PHP installations may use a different php.ini file for command line (CLI) PHP parser. As a result, when running the above yiic commands, you may encounter errors like "YiiBase::include(PDO.php): failed to open stream..." or "...could not find driver". Please double check your CLI PHP configuration by executing the following command:

php -r "phpinfo();"

The result of the above command will show which php.ini file is being used and which extensions are loaded. If a wrong php.ini file is used, you may use the following command to explicitly specify the correct php.ini to use:

php -c php.ini /wwwroot/yii/framework/yiic.php shell /wwwroot/blog/index.php

The commands above accomplish two tasks. First, the model commands generate a model class file for each database table. Second, the crud commands generate the code needed by the CRUD operations for the Post and Comment models.

We can test the generated code by accessing the following URLs:

http://www.example.com/blog/index.php?r=post
http://www.example.com/blog/index.php?r=comment

Notice that the post and comment features implemented by the generated code are completely independent of each other. Also, when creating a new post or comment, we are required to enter information, such as authId and createTime, which in real application should be set by the program. Don't worry. We will fix these problems in the next milestones. For now, we should be fairly satisfied as this prototype already contains most features that we need to implement for the blog application.

To prepare for the next milestones, let's take a closer look at the files generated by the above commands. All the files are generated under /wwwroot/blog/protected. For convenience, we group them into model files, controller files and view files:

  • model files:

    • models/User.php contains the user class that extends from CActiveRecord and can be used to access the User database table;
    • models/Post.php contains the Post class that extends from CActiveRecord and can be used to access the Post database table;
    • models/Tag.php contains the Tag class that extends from CActiveRecord and can be used to access the Tag database table;
    • models/Comment.php contains the Comment class that extends from CActiveRecord and can be used to access the Comment database table;
  • controller file:

    • controllers/PostController.php contains the PostController class which is the controller in charge of all CRUD operations about posts;
    • controllers/CommentController.php contains the CommentController class which is the controller in charge of all CRUD operations about comments;
  • view files:

    • views/post/create.php is the view file that shows an HTML form to create a new post;
    • views/post/update.php is the view file that shows an HTML form to update an existing post;
    • views/post/show.php is the view file that displays the detailed information of a post;
    • views/post/list.php is the view file that displays a list of posts;
    • views/post/admin.php is the view file that displays posts in a table with administrative commands.
    • views/post/_form.php is the partial view file that displays the HTML form for collecting post information. It is embedded in the create and update views.
    • a similar set of view files are also generated for comment.

In order to understand better how the above files are used, we show in the following the workflow that occurs in the blog application when displaying a list of posts:

  1. The entry script is executed by the Web server which creates and initializes an application instance to handle the request;
  2. The application creates an instance of PostController and executes it;
  3. The PostController instance executes the requested list action by calling its actionList() method;
  4. The actionList() method queries database to bring back the list of recent posts;
  5. The actionList() method renders the list view with the post data.
$Id: prototype.scaffold.txt 681 2009-02-16 04:57:01Z qiang.xue $

Be the first person to leave a comment

Please to leave your comment.