Also available in these languages:
English日本語polskiРусский简体中文

Overall Design

Based on the analysis of the requirements, we decide to use the following database tables to store the persistent data for our blog application:

  • tbl_user stores the user information, including username and password.
  • tbl_post stores the blog post information. It mainly consists of the following columns:
    • title: required, title of the post;
    • content: required, body content of the post which uses the Markdown format;
    • status: required, status of the post, which can be one of following values:
      • 1, meaning the post is in draft and is not visible to public;
      • 2, meaning the post is published to public;
      • 3, meaning the post is outdated and is not visible in the post list (still accessible individually, though).
    • tags: optional, a list of comma-separated words categorizing the post.
  • tbl_comment stores the post comment information. Each comment is associated with a post and mainly consists of the following columns:
    • name: required, the author name;
    • email: required, the author email;
    • website: optional, the author website URL;
    • content: required, the comment content in plain text format.
    • status: required, status of the comment, which indicates whether the comment is approved (value 2) or not (value 1).
  • tbl_tag stores post tag frequency information that is needed to implement the tag cloud feature. The table mainly contains the following columns:
    • name: required, the unique tag name;
    • frequency: required, the number of times that the tag appears in posts.
  • tbl_lookup stores generic lookup information. It is essentially a map between integer values and text strings. The former is the data representation in our code, while the latter is the corresponding presentation to end users. For example, we use integer 1 to represent the draft post status and string Draft to display this status to end users. This table mainly contains the following columns:
    • name: the textual representation of the data item that is to be displayed to end users;
    • code: the integer representation of the data item;
    • type: the type of the data item;
    • position: the relative display order of the data item among other items of the same type.

The following entity-relation (ER) diagram shows the table structure and relationships about the above tables.

Entity-Relation Diagram of the Blog Database

Entity-Relation Diagram of the Blog Database

Complete SQL statements corresponding to the above ER diagram may be found in the blog demo. In our Yii installation, they are in the file /wwwroot/yii/demos/blog/protected/data/schema.sqlite.sql.

Info: We name all our table names and column names in lower case. This is because different DBMS often have different case-sensitivity treatment and we want to avoid troubles like this.

We also prefix all our tables with tbl_. This serves for two purposes. First, the prefix introduces a namespace to these tables in case when they need to coexist with other tables in the same database, which often happens in a shared hosting environment where a single database is being used by multiple applications. Second, using table prefix reduces the possibility of having some table names that are reserved keywords in DBMS.

We divide the development of our blog application into the following milestones.

  • Milestone 1: creating a prototype of the blog system. It should consist of most of the required functionalities.
  • Milestone 2: completing post management. It includes creating, listing, showing, updating and deleting posts.
  • Milestone 3: completing comment management. It includes creating, listing, approving, updating and deleting post comments.
  • Milestone 4: implementing portlets. It includes user menu, login, tag cloud and recent comments portlets.
  • Milestone 5: final tune-up and deployment.
$Id: start.design.txt 1677 2010-01-07 20:29:26Z qiang.xue $
If you find any typos or errors in the tutorial, please create a Yii ticket to report it. If it is a translation error, please create a Yiidoc ticket, instead. Thank you.

Total 8 comments:

#243
Why repeat tag information in the posts table?
by tim at 4:59pm on April 28, 2009.

Just curious why you would store the tag information in the posts table in addition to using the lookup table to store the same info (linking the postID to the tagIDs).

Given the obvious redundancy I'm guessing it's to simplify queries or speed up performance? Can someone explain what the benefit is to this approach?

To me it seems like a potential maintenance problem. For example, imagine you want to delete a tag from the tags table (and therefore all references to it in all of the posts). How would you go about updating all the posts to remove that tag when it's stored as part of a comma separated list in one of the posts table fields?

#451
How to draw this Entity-Relation Diagram?
by swdonline at 7:09am on July 7, 2009.

Can you tell me what tool can draw this type of Entity-Relation Diagram?

#497
ERD
by phazei at 7:07pm on July 23, 2009.

MySQL Workbench is wonderful for designing and creating ERD's. There is an OSS free version.

#648
camelCase tablenames?
by mech7 at 9:54pm on September 6, 2009.

Is the use of camelCase not frowned upon? as it causes problems.. for example if I import the sql in phpmyadmin it will all be lowercase..

#1084
many-to-many relations
by phuongle at 2:07pm on February 8, 2010.

I wonder why the authors didn't give an example about many-to-many relationship. In old blog tutorial for Yii 1.0, there is table post_tag to demonstrate how to implement many-many relationship to your application.

I hope someone will make a similar tutorial on that topic for Yii 1.1

#1324
True
by colt at 3:14am on March 27, 2010.

I agree on that 100% :) Many-to-many is often met on legacy DB and so on.

#1472
many-to-may: absolutely needed in an example
by Wabiloo at 5:01am on May 10, 2010.

I agree wholeheartedly with the previous 2 posters. Many-to-many relationships are a reality out there, and one of the most mightmarish things to address in ORM. An example that does not indicate how to work with this sort of relationship fails to convince the user that the framework is solid, powerful and flexible... Bring back the many-to-many example, I say...

#1473
many-to-may: absolutely needed in an example
by Wabiloo at 5:02am on May 10, 2010.

I agree wholeheartedly with the previous 2 posters. Many-to-many relationships are a reality out there, and one of the most mightmarish things to address in ORM. An example that does not indicate how to work with this sort of relationship fails to convince the user that the framework is solid, powerful and flexible... Bring back the many-to-many example, I say...

Your Comment:

You may enter comment using Markdown syntax.

Please login with your forum account.
Note: you must have at least ONE forum post with your account.