From 03f362a40a8dd39f8c8b4bf816334922b7b264e4 Mon Sep 17 00:00:00 2001 From: wei <> Date: Tue, 9 Jan 2007 10:42:06 +0000 Subject: add TActiveRecord::findAllByPks() --- .../protected/pages/Database/ActiveRecord.page | 280 ++++++++++++--------- 1 file changed, 158 insertions(+), 122 deletions(-) (limited to 'demos/quickstart/protected/pages/Database/ActiveRecord.page') diff --git a/demos/quickstart/protected/pages/Database/ActiveRecord.page b/demos/quickstart/protected/pages/Database/ActiveRecord.page index e3da53c0..a1337ba3 100644 --- a/demos/quickstart/protected/pages/Database/ActiveRecord.page +++ b/demos/quickstart/protected/pages/Database/ActiveRecord.page @@ -2,53 +2,53 @@
Active Records are objects that wrap a row in a database table or view, - encapsulates the database access and adds domain logic on that data. - The basics of an Active Record is a business object class, e.g., a - Products class, that match very closely the record structure - of an underlying database table. Each Active Record will be responsible for - saving and loading data to and from the database.
+ encapsulates the database access and adds domain logic on that data. + The basics of an Active Record is a business object class, e.g., a + Products class, that match very closely the record structure + of an underlying database table. Each Active Record will be responsible for + saving and loading data to and from the database.Active Record is a good choice for domain logic that isn't too complex, - such as creates, reads, updates, and deletes. Derivations and validations - based on a single record work well in this structure. Active Record has the - primary advantage of simplicity. It's easy to build - Active Records, and they are easy to understand.
+ such as creates, reads, updates, and deletes. Derivations and validations + based on a single record work well in this structure. Active Record has the + primary advantage of simplicity. It's easy to build + Active Records, and they are easy to understand. -However, as your business logic grows in complexity, you'll soon want - to use your object's direct relationships, collections, inheritance, and so +
However, as your business logic grows in complexity, you'll soon want + to use your object's direct relationships, collections, inheritance, and so forth. These don't map easily onto Active Record, and adding them piecemeal - gets very messy. - Another argument against Active Record is the fact that it couples the object - design to the database design. This makes it more difficult to refactor as a project goes forward.
- -The alternative is to use a Data Mapper that separates the roles of the - business object and how these objects are stored. - Prado provides a complimentary choice between Active Record and - SqlMap Data Mapper. - A SqlMap Data Mapper can be used to load Active Record objects, in turn, these - Active Record objects can be used to update the database. - The "relationship" between Active Records and SqlMap is illustrated in the - following diagram. More details regarding the SqlMap Data Mapper can be found in - the SqlMap Manual. - alt="Active Records and SqlMap DataMapper" id="fig:diagram.png" class="figure"/> -
- -- The Active Record class has methods that do the following: -
The alternative is to use a Data Mapper that separates the roles of the + business object and how these objects are stored. + Prado provides a complimentary choice between Active Record and + SqlMap Data Mapper. + A SqlMap Data Mapper can be used to load Active Record objects, in turn, these + Active Record objects can be used to update the database. + The "relationship" between Active Records and SqlMap is illustrated in the + following diagram. More details regarding the SqlMap Data Mapper can be found in + the SqlMap Manual. + alt="Active Records and SqlMap DataMapper" id="fig:diagram.png" class="figure"/> +
+ ++ The Active Record class has methods that do the following: +
Let us
- consider the following "users" table that contains two columns named "username" and "email",
- where "username" is also the primary key.
+ consider the following "users" table that contains two columns named "username" and "email",
+ where "username" is also the primary key.
Each property of the UserRecord class must correspond to a - column with the same name in the "users" table. The static class variable - $_tablename (must be public) is optional when the class name is the same as - the table name in the database, otherwise $_tablename must - specify the table name that corresponds to your Active Record class. + column with the same name in the "users" table. The static class variable + $_tablename (must be public) is optional when the class name is the same as + the table name in the database, otherwise $_tablename must + specify the table name that corresponds to your Active Record class.
- The static method finder() returns an UserRecord instance - that can be used to load records from the database. The loading of records - using the finer methods is discuss a little later. The TActiveRecord::getRecordFinder() - static method takes the name of the current Active Record class as parameter. + The static method finder() returns an UserRecord instance + that can be used to load records from the database. The loading of records + using the finer methods is discuss a little later. The TActiveRecord::getRecordFinder() + static method takes the name of the current Active Record class as parameter.
- A default database connection for Active Record can be set as follows.
- See Establishing Database Connection for
- futher details regarding creation of database connection in general.
+ A default database connection for Active Record can be set as follows.
+ See Establishing Database Connection for
+ futher details regarding creation of database connection in general.
- The default database connection can also be configured using a <module>
- tag in the application.xml
- or config.xml as follows.
+ The default database connection can also be configured using a <module>
+ tag in the application.xml
+ or config.xml as follows.
A ConnectionID property can be specified with value corresponding
- to another TDataSourceConfig module configuration's ID value. This allows
- the same database connection to be used in other modules such as SqlMap.
+ to another TDataSourceConfig module configuration's ID value. This allows
+ the same database connection to be used in other modules such as SqlMap.
- The TActiveRecord class provides many convenient methods to find
- records from the database. The simplest is finding records by matching primary keys.
- See the
Finds one record using only the primary key or composite primary keys. +
Finds one record using only the primary key or composite primary keys.
Finds multiple records using a list of primary keys or composite primary keys.
+The following are equivalent for scalar primary keys (primary key consisting of only one column/field).
+
Finds one single record that matches the criteria. The criteria
- can be a partial SQL string or a TActiveRecordCriteria object.
+ can be a partial SQL string or a TActiveRecordCriteria object.
The TActiveRecordCriteria class has the following properties: -
Finds records using full SQL, returns corresponding array of record objects.
@@ -291,7 +318,7 @@ incremented values.
To update a record in the database, just change one or more properties of
the Active Record object that has been loaded from the database and then
-call the save() method.
+call the save() method.
Active Record objects have a simple life-cycle illustrated in the following diagram.
alt="Active Records Life Cycle" id="fig:cycle.png" class="figure"/>
+We see that new ActiveRecord objects are created by either using one of the find*()
+methods or using creating a new instance by using PHP's new keyword. Objects
+created by a find*() method starts with clean state. New instance of
+ActiveRecords created other than by a find*() method starts with new state.
+When ever you
+call the save() method on the ActiveRecord object, the object enters the clean
+state. Objects in the clean becomes dirty whenever one of more of its
+internal states are changed. Calling the delete() method on the object
+ends the object life-cycle, no futher actions can be performed on the object.
- To delete an existing record that is already loaded, just call the delete() method.
- You can also delete records in the database by primary keys without
- loading any records using the deleteByPk() method.
- For example, to delete one or records with tables having a scalar primary key.
+ To delete an existing record that is already loaded, just call the delete() method.
+ You can also delete records in the database by primary keys without
+ loading any records using the deleteByPk() method.
+ For example, to delete one or records with tables having a scalar primary key.
All Active Record objects contains the property DbConnection
- that can be used to obtain a transaction object.
+ that can be used to obtain a transaction object.
Deleting existing records
Transactions
References
-
\ No newline at end of file
--
cgit v1.2.3