diff options
author | xue <> | 2007-04-05 19:53:01 +0000 |
---|---|---|
committer | xue <> | 2007-04-05 19:53:01 +0000 |
commit | 16bbf435c8048c3c726de78adc8a670b8a527229 (patch) | |
tree | 8884afb6f7d706a758ac7a6775c897e2b633d308 /demos | |
parent | 791acbbebad01a33cce22209ea561b282ee876e4 (diff) |
Added Day2 for blog-tutorial.
Diffstat (limited to 'demos')
-rw-r--r-- | demos/blog-tutorial/protected/pages/Day2/ConnectDB.page | 37 | ||||
-rw-r--r-- | demos/blog-tutorial/protected/pages/Day2/CreateDB.page | 41 | ||||
-rw-r--r-- | demos/blog-tutorial/protected/pages/Day2/ER.gif | bin | 4896 -> 4919 bytes | |||
-rw-r--r-- | demos/blog-tutorial/protected/pages/Day2/ER.vsd | bin | 73728 -> 73216 bytes | |||
-rw-r--r-- | demos/blog-tutorial/protected/pages/Day2/directories.gif | bin | 0 -> 4580 bytes |
5 files changed, 66 insertions, 12 deletions
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 @@ <h1>Establishing DB Connection</h1>
<p>
+To use the database that we just created, we first need to establish a connection to it.
</p>
+<p>
+We are going to use <a href="http://www.pradosoft.com/demos/quickstart/?page=Database.DAO">Data Access Objects (DAO)</a> 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.
+</p>
+
+<com:NoteBox>
+To use DAO, you have to install and enable the <a href="http://www.php.net/manual/en/ref.pdo.php">PHP PDO extension</a> <i>and</i> a database-specific PDO driver (in our case, it is the SQLite PDO driver). This can be achieved easily on Windows by modifying the <tt>php.ini</tt> file with the following lines:
+<com:TTextHighlighter CssClass="source">
+extension=php_pdo.dll
+extension=php_pdo_sqlite.dll
+</com:TTextHighlighter>
+</com:NoteBox>
+
+<p>
+To further reduce the dependency on the actual database tables, we will also use the <a href="http://www.pradosoft.com/demos/quickstart/?page=Database.ActiveRecord">Active Record</a> 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.
+</p>
+
+<p>
+We modify our application configuration file <tt>protected/application.xml</tt> by inserting the following lines, which tells Active Record how to connect to our newly created database:
+</p>
+
+<com:TTextHighlighter CssClass="source" Language="xml">
+<modules>
+ <module class="System.Data.ActiveRecord.TActiveRecordConfig">
+ <database ConnectionString="sqlite:protected/data/blog.db" />
+ </module>
+</modules>
+</com:TTextHighlighter>
+
+<p>
+The configuration above shows that we are adding to our application a <a href="http://www.pradosoft.com/demos/quickstart/?page=Fundamentals.Modules">module</a> whose class is specified in the <a href="http://www.pradosoft.com/demos/quickstart/?page=Fundamentals.Components">namespace</a> format as <tt>System.Data.ActiveRecord.TActiveRecordConfig</tt>. Through this module, Active Record will automatically establish a DB connection by using the connection information given in <tt>ConnectionString</tt>.
+</p>
+
+<com:InfoBox>
+One may set up two or more DB connections in the application configuration. For more details, see the <a href="http://www.pradosoft.com/demos/quickstart/?page=Database.ActiveRecord">Active Record Documentation</a>. And of course, one may also explicitly create a DB connection in PHP code using the <a href="http://www.pradosoft.com/demos/quickstart/?page=Database.DAO">TDbConnection</a> component in PDO.
+</com:InfoBox>
+
</com:TContent>
\ 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 @@ <h1>Creating Database</h1>
<p>
-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 <a href="http://www.pradosoft.com/demos/quickstart/?page=Database.DAO">database access object (DAO)</a> and <a href="http://www.pradosoft.com/demos/quickstart/?page=Database.ActiveRecord">active record</a>.
+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 <a href="http://www.pradosoft.com/demos/quickstart/?page=Database.DAO">database access object (DAO)</a> and <a href="http://www.pradosoft.com/demos/quickstart/?page=Database.ActiveRecord">Active Record</a>.
</p>
<p>
@@ -13,34 +13,51 @@ For tutorial purpose, we have simplified the requirements of our blog system so <img src="<%~ ER.gif %>" />
<p>
-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 <tt>protected/schema.sql</tt>.
</p>
<com:TTextHighlighter CssClass="source">
+/* 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');
+</com:TTextHighlighter>
+
+<com:NoteBox>
+SQLite does not support <a href="http://www.sqlite.org/omitted.html">foreign key constraint</a>. Therefore, we will write PHP code to ensure that the <tt>posts.author</tt> field contains valid data. Also, we are exploiting the fact that the <tt>posts.post_id</tt> field is <a href="http://www.sqlite.org/autoinc.html">auto-incremental</a> if we assign NULL to it.
+</com:NoteBox>
+
+<p>
+We use the <a href="http://www.sqlite.org/download.html">SQLite command line tool</a> to create the SQLite database. We first create a directory <tt>protected/data</tt> 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 <tt>protected/data</tt>:
+</p>
+
+<com:TTextHighlighter CssClass="source">
+sqlite3 blog.db < ../schema.sql
</com:TTextHighlighter>
<p>
-We use a SQLite 3 database to keep our data.
+The database has been created as <tt>protected/data/blog.db</tt> and we shall see the following directories and files:
</p>
+<img src="<%~ directories.gif %>" />
</com:TContent>
\ 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 Binary files differindex 53a7a695..13e2d15b 100644 --- a/demos/blog-tutorial/protected/pages/Day2/ER.gif +++ b/demos/blog-tutorial/protected/pages/Day2/ER.gif diff --git a/demos/blog-tutorial/protected/pages/Day2/ER.vsd b/demos/blog-tutorial/protected/pages/Day2/ER.vsd Binary files differindex 52a08530..2b59897a 100644 --- a/demos/blog-tutorial/protected/pages/Day2/ER.vsd +++ b/demos/blog-tutorial/protected/pages/Day2/ER.vsd diff --git a/demos/blog-tutorial/protected/pages/Day2/directories.gif b/demos/blog-tutorial/protected/pages/Day2/directories.gif Binary files differnew file mode 100644 index 00000000..797ef932 --- /dev/null +++ b/demos/blog-tutorial/protected/pages/Day2/directories.gif |