From 45b0fe42a979d444d547a5248eb2e9e915aaf16a Mon Sep 17 00:00:00 2001
From: wei <>
Date: Sun, 14 Jan 2007 02:10:24 +0000
Subject: Add "block-content" to allow user comments on block level elements in
quickstart docs.
---
.../protected/pages/Database/ActiveRecord.page | 139 +++++++++++----------
1 file changed, 70 insertions(+), 69 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 4dd70608..bc1d2359 100644
--- a/demos/quickstart/protected/pages/Database/ActiveRecord.page
+++ b/demos/quickstart/protected/pages/Database/ActiveRecord.page
@@ -1,7 +1,7 @@
Active Records are objects that wrap a row in a database table or view,
+ 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
@@ -13,21 +13,21 @@
Each field in the class must correspond to one column in the table.
- Active Record is a good choice for domain logic that isn't too complex,
+ 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. However, as your business logic grows in complexity, you'll soon want
+ 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
+ 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.
@@ -39,26 +39,28 @@
+
The Active Record class has methods that do the following:
- Active Record
-Active Record
+When to Use It
-When to Use It
+ alt="Active Records and SqlMap DataMapper" id="fig:diagram.png" class="figure"/>
+
The Active Record implementation utilizes the Prado DAO classes for data access. The current Active Record implementation supports MySQL, Postgres SQL and SQLite databases. Support for other databases can be provided when there are sufficient demand. -
Let us +
+Let us
consider the following "users" table that contains two columns named "username" and "email",
where "username" is also the primary key.
-
Next we define our Active Record class that corresponds to the "users" table.
- Next we define our Active Record class that corresponds to the "users" table.
+ Each property of the UserRecord class must correspond to a
+ 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
@@ -102,7 +104,7 @@ E.g. MySQL uses back-ticks, $_tablename = "`database1`.`table1`"
Since TActiveRecord extends TComponent, setter and
getter methods can be defined to allow control over how variables
are set and returned. For example, adding a $level property to the UserRecord 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.
+
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.
- A ConnectionID property can be specified with value corresponding
+ 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.
-
+
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.
+ 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.
- Finds one single record that matches the criteria. The criteria
+ can be a partial SQL string or a TActiveRecordCriteria object. The TActiveRecordCriteria class has the following properties:
- The TActiveRecordCriteria class has the following properties:
+ Same as find() but returns an array of objects. Same as find() but returns an array of objects. Dynamic find method using parts of method name as search criteria.
+ Dynamic find method using parts of method name as search criteria.
Method names starting with findBy return 1 record only.
Method names starting with findAllBy return an array of records.
The condition is taken as part of the method name after findBy or findAllBy.
The following blocks of code are equivalent:
- Finds records using full SQL, returns corresponding array of record objects. Finds records using full SQL, returns corresponding array of record objects. Find the number of matchings records. Find the number of matchings records.
+
Add a new record using TActiveRecord is very simple, just create a new Active
Record object and call the save() method. E.g.
-
+
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.
-
+
Active Record objects have a simple life-cycle illustrated in the following diagram.
+
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.
-
+
For composite primary keys (determined automatically from the table definitions):
- All Active Record objects contains the property DbConnection
+ All Active Record objects contains the property DbConnection
that can be used to obtain a transaction object.
-Setting up a database connection
-Setting up a database connection
+Loading data from the database
-Loading data from the database
+findByPk()
- findByPk()
+ findAllByPks()
- findAllByPks()
+ find()
-find()
+
+
-findAll()
-findAll()
+findBy*() and findAllBy*()
-findBy*() and findAllBy*()
+findBySql()
-findBySql()
+count()
-count()
+Inserting and updating records
-Inserting and updating records
+ 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*()
@@ -346,22 +347,22 @@ 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.
Deleting existing records
-Deleting existing records
+Transactions
-Transactions
+References
-
+
References
+
--
cgit v1.2.3