From d0bdd3144dfc972450d79ddaf6197a30b27eacc0 Mon Sep 17 00:00:00 2001 From: xue <> Date: Fri, 6 Apr 2007 12:52:54 +0000 Subject: Fixed some issues in day 2. --- .../protected/pages/Day2/ConnectDB.page | 7 +-- .../protected/pages/Day2/CreateAR.page | 55 ++++++++++++++++++---- .../samples/day2/blog/protected/application.xml | 8 ++-- .../day2/blog/protected/database/PostRecord.php | 2 +- .../day2/blog/protected/database/UserRecord.php | 2 +- 5 files changed, 54 insertions(+), 20 deletions(-) (limited to 'demos') diff --git a/demos/blog-tutorial/protected/pages/Day2/ConnectDB.page b/demos/blog-tutorial/protected/pages/Day2/ConnectDB.page index 90db11ef..1d036577 100644 --- a/demos/blog-tutorial/protected/pages/Day2/ConnectDB.page +++ b/demos/blog-tutorial/protected/pages/Day2/ConnectDB.page @@ -28,18 +28,19 @@ We modify our application configuration file protected/application.xml - + +

-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. +The configuration above shows that we are adding two modules to our application. The TDataSourceConfig module is configured with the connection string sqlite:protected/data/blog.db which points to our SQLite database. This connection is used by the TActiveRecordConfig module which is required by Active Record.

-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. +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/CreateAR.page b/demos/blog-tutorial/protected/pages/Day2/CreateAR.page index bf2c05aa..d8b8b8ce 100644 --- a/demos/blog-tutorial/protected/pages/Day2/CreateAR.page +++ b/demos/blog-tutorial/protected/pages/Day2/CreateAR.page @@ -3,9 +3,19 @@

Creating Active Record Classes

-We need to create two Active Record classes, UserRecord and PostRecord, to represent data records in the users and posts tables, respectively. Active Record classes must extend from the base class ActiveRecord, and their properties must match exactly to the fields of the corresponding tables. To better organize our directories, we create a new directory protected/database to hold the class files. +We need to create two Active Record classes, UserRecord and PostRecord, to represent data records in the users and posts tables, respectively. Active Record classes must extend from the base class ActiveRecord, and their properties must match exactly to the fields of the corresponding tables.

+

+To better organize our directories, we create a new directory protected/database to hold the class files. We also modify our application configuration by inserting the following lines. It is equivalent to adding the directory protected/database to PHP include_path, which allows us to use the classes without explicitly including them. +

+ + + + + + +

Instead of writing the classes manually, we will use the PRADO command line tool again to generate the classes for us.

@@ -54,19 +64,18 @@ We should see the following directory structure with two new files under pro

-If we check the UserRecord class file, we should see the following content. As we see, for each field in the users table, the class has a corresponding data member. The constant TABLE specifies the table name for the UserRecord. The static method finder() allows us to perform query and retrieve users data in terms of UserRecord objects, as we will see in later sections. +If we check the PostRecord class file, we should see the following content.

-class UserRecord extends TActiveRecord +class PostRecord extends TActiveRecord { - const TABLE='users'; - public $username; - public $email; - public $password; - public $role; - public $first_name; - public $last_name; + const TABLE='posts'; + public $post_id; + public $author; + public $create_time; + public $title; + public $content; public static function finder($className=__CLASS__) { @@ -75,4 +84,30 @@ class UserRecord extends TActiveRecord } +

+As we see, for each field in the posts table, the class has a corresponding data member. The constant TABLE specifies the table name for the PostRecord. The static method finder() allows us to perform query and retrieve post data in terms of PostRecord objects. +

+ +

+We can use the command line tool to do some testing with our newly created Active Record classes. Still in the interactive mode of the command line tool, we enter a PHP statement and should see the following. Interested readers may try some other PHP statements, such as UserRecord::finder()->findAll(). +

+ + +>> PostRecord::finder()->findAll() +array +( + [0] => PostRecord#1 + ( + [post_id] => '1' + [author] => 'admin' + [create_time] => '1175708482' + [title] => 'first post' + [content] => 'this is my first post' + [TActiveRecord:_readOnly] => false + [TActiveRecord:_connection] => null + [TComponent:_e] => array() + ) +) + + \ No newline at end of file diff --git a/demos/blog-tutorial/samples/day2/blog/protected/application.xml b/demos/blog-tutorial/samples/day2/blog/protected/application.xml index 69cdbf9f..503b625a 100644 --- a/demos/blog-tutorial/samples/day2/blog/protected/application.xml +++ b/demos/blog-tutorial/samples/day2/blog/protected/application.xml @@ -1,12 +1,9 @@ - @@ -23,9 +20,10 @@ --> - + + diff --git a/demos/blog-tutorial/samples/day2/blog/protected/database/PostRecord.php b/demos/blog-tutorial/samples/day2/blog/protected/database/PostRecord.php index a9fb5db3..31451b30 100644 --- a/demos/blog-tutorial/samples/day2/blog/protected/database/PostRecord.php +++ b/demos/blog-tutorial/samples/day2/blog/protected/database/PostRecord.php @@ -1,6 +1,6 @@