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 must define property names that matches with the field names of the corresponding table.

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.

Under the blog directory, run the following command to enter into the interactive mode of the command line tool:

php path/to/prado-cli.php shell .

We should see

Command line tools for Prado 3.1.0. ** Loaded PRADO appplication in directory "protected". PHP-Shell - Version 0.3.1 (c) 2006, Jan Kneschke >> use '?' to open the inline help >>

At the prompt, enter the following two commands to create UserRecord and PostRecord classes:

>> generate users Application.database.UserRecord >> generate posts Application.database.PostRecord

Here we used the namespace format again to specify the classes to be created. The path Application.database.UserRecord indicates that we want the UserRecord class file to be protected/database/UserRecord.php.

We should see the following directory structure with two new files under protected/database:

If we check the PostRecord class file, we should see the following content.

class PostRecord extends TActiveRecord { const TABLE='posts'; public $post_id; public $author_id; public $create_time; public $title; public $content; public $status; public static function finder($className=__CLASS__) { return parent::finder($className); } }

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_id] => 'admin' [create_time] => '1175708482' [title] => 'first post' [content] => 'this is my first post' [status] => '0' [TActiveRecord:_readOnly] => false [TActiveRecord:_connection] => null [TComponent:_e] => array() ) )

Relationship Between Posts and Users

Recall that there was a foreign key relationship between the users and posts table. The entity-relationship diagram is shown below for convienence.

From the entity-relationship diagram above, we see that the posts table contains a field named author_id. This author_id field is a foreign key to the reference table users. In particular, the values in the author_id field should be of that from the users table's username field. One of the consequence of this relationship, thinking in terms of objects, is that each "post" belongs to an "author" and one "author" may have many "posts".

We can model the relationship between posts and users table in Active Record by modifying the PostRecord and UserRecord classes as follows.

class PostRecord extends TActiveRecord { //... properties and methods as before public $author; //holds an UserRecord public static $RELATIONS=array ( 'author' => array(self::BELONGS_TO, 'UserRecord'), ); }

The static $RELATIONS property of PostRecord defines that the property $author belongs to an UserRecord. In array(self::BELONGS_TO, 'UserRecord'), the first element defines the relationship type, in this case self::BELONGS_TO. The second element is the name of related record, in this case an UserRecord. The UserRecord is defined similarly below, the difference is that, the user record has many PostRecords.

class UserRecord extends TActiveRecord { //... properties and methods as before public $posts=array(); //holds an array of PostRecord public static $RELATIONS=array ( 'posts' => array(self::HAS_MANY, 'PostRecord'), ); }

An array of UserRecord with and its corresponding posts may be fetched as follows.

$users = UserRecord::finder()->withPosts()->findAll(); The method withXXX() (where XXX is the relationship property name, in this case, Posts) fetches the corresponding PostRecords using a second query (not by using a join). The withXXX() method accepts the same arguments as other finder methods of TActiveRecord, e.g. withPosts('status = ?', 0).

Further detailed documentation can be found in the quickstart Active Record docs.