From 16bbf435c8048c3c726de78adc8a670b8a527229 Mon Sep 17 00:00:00 2001 From: xue <> Date: Thu, 5 Apr 2007 19:53:01 +0000 Subject: Added Day2 for blog-tutorial. --- .../protected/pages/Day2/ConnectDB.page | 37 +++++++++++++++++++ .../protected/pages/Day2/CreateDB.page | 41 +++++++++++++++------ demos/blog-tutorial/protected/pages/Day2/ER.gif | Bin 4896 -> 4919 bytes demos/blog-tutorial/protected/pages/Day2/ER.vsd | Bin 73728 -> 73216 bytes .../protected/pages/Day2/directories.gif | Bin 0 -> 4580 bytes 5 files changed, 66 insertions(+), 12 deletions(-) create mode 100644 demos/blog-tutorial/protected/pages/Day2/directories.gif (limited to 'demos/blog-tutorial/protected/pages') diff --git a/demos/blog-tutorial/protected/pages/Day2/ConnectDB.page b/demos/blog-tutorial/protected/pages/Day2/ConnectDB.page index 26d8a751..232ceb98 100644 --- a/demos/blog-tutorial/protected/pages/Day2/ConnectDB.page +++ b/demos/blog-tutorial/protected/pages/Day2/ConnectDB.page @@ -3,6 +3,43 @@

Establishing DB Connection

+To use the database that we just created, we first need to establish a connection to it.

+

+We are going to use Data Access Objects (DAO) to abstract our data access mechanisms. If in future we decide to use a different DBMS (e.g. PostgreSQL, Oracle) to store our blog data, we only need to change the database source name (DSN) and we can keep our PHP code intact. +

+ + +To use DAO, you have to install and enable the PHP PDO extension and a database-specific PDO driver (in our case, it is the SQLite PDO driver). This can be achieved easily on Windows by modifying the php.ini file with the following lines: + +extension=php_pdo.dll +extension=php_pdo_sqlite.dll + + + +

+To further reduce the dependency on the actual database tables, we will also use the Active Record feature which is based on PDO. Each data record will be represented as an Active Record object, which saves us from writing repetitive SQL statements. +

+ +

+We modify our application configuration file protected/application.xml by inserting the following lines, which tells Active Record how to connect to our newly created database: +

+ + + + + + + + + +

+The configuration above shows that we are adding to our application a module whose class is specified in the namespace format as System.Data.ActiveRecord.TActiveRecordConfig. Through this module, Active Record will automatically establish a DB connection by using the connection information given in ConnectionString. +

+ + +One may set up two or more DB connections in the application configuration. For more details, see the Active Record Documentation. And of course, one may also explicitly create a DB connection in PHP code using the TDbConnection component in PDO. + + \ No newline at end of file diff --git a/demos/blog-tutorial/protected/pages/Day2/CreateDB.page b/demos/blog-tutorial/protected/pages/Day2/CreateDB.page index 04dd0d90..0d45ba50 100644 --- a/demos/blog-tutorial/protected/pages/Day2/CreateDB.page +++ b/demos/blog-tutorial/protected/pages/Day2/CreateDB.page @@ -3,7 +3,7 @@

Creating Database

-Most Web applications use database to keep data. Our blog system is not an exception. In this section, we will describe how to write database-driven pages for our blog system. We will use techniques including database access object (DAO) and active record. +Most Web applications use database to keep data. Our blog system is not an exception. In this section, we will describe how to write database-driven pages for our blog system. We will use techniques including database access object (DAO) and Active Record.

@@ -13,34 +13,51 @@ For tutorial purpose, we have simplified the requirements of our blog system so

-The corresponding SQL statements for creating the tables are as follows, +We use a SQLite 3 database to keep our data. We first convert the ER diagram into the following SQL statements and save them in the file protected/schema.sql.

+/* create users table */ CREATE TABLE users ( username VARCHAR(128) NOT NULL PRIMARY KEY, email VARCHAR(128) NOT NULL UNIQUE, - password VARCHAR(128) NOT NULL, + password VARCHAR(128) NOT NULL, /* plain text password */ + role INTEGER NOT NULL, /* 0: normal user, 1: administrator */ first_name VARCHAR(128), last_name VARCHAR(128) ); +/* create posts table */ CREATE TABLE posts ( post_id INTEGER NOT NULL PRIMARY KEY, - author VARCHAR(128) NOT NULL, - create_time INTEGER NOT NULL, - title VARCHAR(256) NOT NULL, - content TEXT NOT NULL, - CONSTRAINT "posts_fk" FOREIGN KEY ("author") - REFERENCES users ("username") - ON DELETE CASCADE - ON UPDATE CASCADE + author VARCHAR(128) NOT NULL, /* references users.username */ + create_time INTEGER NOT NULL, /* UNIX timestamp */ + title VARCHAR(256) NOT NULL, /* title of the post */ + content TEXT NOT NULL /* content of the post */ ); + +/* insert some initial data records for testing */ +INSERT INTO users VALUES ('admin', 'admin@example.com', 'demo', 1, 'Qiang', 'Xue'); +INSERT INTO users VALUES ('demo', 'demo@example.com', 'demo', 0, 'Wei', 'Zhuo'); +INSERT INTO posts VALUES (NULL, 'admin', 1175708482, 'first post', 'this is my first post'); + + + +SQLite does not support foreign key constraint. Therefore, we will write PHP code to ensure that the posts.author field contains valid data. Also, we are exploiting the fact that the posts.post_id field is auto-incremental if we assign NULL to it. + + +

+We use the SQLite command line tool to create the SQLite database. We first create a directory protected/data to hold the SQLite database file. Per SQLite's requirement, this directory and the database file to be created must be set writable by the Web server process. We now execute the following command under the directory protected/data: +

+ + +sqlite3 blog.db < ../schema.sql

-We use a SQLite 3 database to keep our data. +The database has been created as protected/data/blog.db and we shall see the following directories and files:

+ \ No newline at end of file diff --git a/demos/blog-tutorial/protected/pages/Day2/ER.gif b/demos/blog-tutorial/protected/pages/Day2/ER.gif index 53a7a695..13e2d15b 100644 Binary files a/demos/blog-tutorial/protected/pages/Day2/ER.gif and b/demos/blog-tutorial/protected/pages/Day2/ER.gif differ diff --git a/demos/blog-tutorial/protected/pages/Day2/ER.vsd b/demos/blog-tutorial/protected/pages/Day2/ER.vsd index 52a08530..2b59897a 100644 Binary files a/demos/blog-tutorial/protected/pages/Day2/ER.vsd and b/demos/blog-tutorial/protected/pages/Day2/ER.vsd differ diff --git a/demos/blog-tutorial/protected/pages/Day2/directories.gif b/demos/blog-tutorial/protected/pages/Day2/directories.gif new file mode 100644 index 00000000..797ef932 Binary files /dev/null and b/demos/blog-tutorial/protected/pages/Day2/directories.gif differ -- cgit v1.2.3