From 199fc1254f84f851a2894df94487a45ed68f7c98 Mon Sep 17 00:00:00 2001 From: xue <> Date: Fri, 28 Sep 2007 18:08:20 +0000 Subject: Fixed #665. --- .../protected/pages/Database/ActiveRecord.page | 57 ++++++++++++++++++---- .../pages/GettingStarted/NewFeatures.page | 1 + 2 files changed, 49 insertions(+), 9 deletions(-) (limited to 'demos/quickstart') diff --git a/demos/quickstart/protected/pages/Database/ActiveRecord.page b/demos/quickstart/protected/pages/Database/ActiveRecord.page index 481985e9..a28c3d5e 100644 --- a/demos/quickstart/protected/pages/Database/ActiveRecord.page +++ b/demos/quickstart/protected/pages/Database/ActiveRecord.page @@ -52,21 +52,21 @@

Design Implications

-Prado's implementation of Active Record does not maintain referential identity. Each object obtained using -Active Record is a copy of the data in the database. For example, If you ask for -a particular customer and get back a Customer object, the next time you ask for +Prado's implementation of Active Record does not maintain referential identity. Each object obtained using +Active Record is a copy of the data in the database. For example, If you ask for +a particular customer and get back a Customer object, the next time you ask for that customer you get back another instance of a Customer object. This implies that a strict comparison (i.e., using ===) will return false, while loose comparison (i.e., using ==) will -return true if the object values are equal by loose comparison. +return true if the object values are equal by loose comparison.

This is design implication related to the following question. -"Do you think of the customer as an object, of which there's only one, +"Do you think of the customer as an object, of which there's only one, or do you think of the objects you operate on as copies of the database?" -Other O/R mappings will imply that there is only one Customer object with custID 100, and it literally is that customer. -If you get the customer and change a field on it, then you have now changed that customer. -"That constrasts with: you have changed this copy of the customer, but not that copy. -And if two people update the customer on two copies of the object, whoever updates first, +Other O/R mappings will imply that there is only one Customer object with custID 100, and it literally is that customer. +If you get the customer and change a field on it, then you have now changed that customer. +"That constrasts with: you have changed this copy of the customer, but not that copy. +And if two people update the customer on two copies of the object, whoever updates first, or maybe last, wins." [A. Hejlsberg 2003]

@@ -1059,6 +1059,45 @@ arrays. E.g. $player->skills[] = new SkillRecord(). If array w will be thrown.

+

Column Mapping

+

+Since v3.1.1, Active Record starts to support column mapping. Column mapping allows developers +to address columns in Active Record using a more consistent naming convention. In particular, +using column mapping, one can access a column using whatever name he likes, rather than limited by +the name defined in the database schema. +

+

+To use column mapping, declare a static array named COLUMN_MAPPING in the Active Record class. +The keys of the array are column names (called physical column names) as defined in the database +schema, while the values are corresponding property names (called logical column names) defined +in the Active Record class. The property names can be either public class member variable names or +component property names defined via getters/setters. If a physical column name happens to be the same +as the logical column name, they do not need to be listed in COLUMN_MAPPING. +

+ +class UserRecord extends TActiveRecord +{ + const TABLE='users'; + protected static $COLUMN_MAPPING=array + ( + 'user_id'=>'id', + 'email_address'=>'email', + 'first_name'=>'firstName', + 'last_name'=>'lastName', + ); + public $id; + public $username; // the physical and logical column names are the same + public $email; + public $firstName; + public $lastName; + //.... +} + +

+With the above column mapping, we can address first_name using $userRecord->firstName +instead of $userRecord->first_name. This helps separation of logic and model. +

+

References

Version 3.1.0

-- cgit v1.2.3