summaryrefslogtreecommitdiff
path: root/demos/quickstart/protected/pages/Database/ActiveRecord.page
diff options
context:
space:
mode:
Diffstat (limited to 'demos/quickstart/protected/pages/Database/ActiveRecord.page')
-rw-r--r--demos/quickstart/protected/pages/Database/ActiveRecord.page139
1 files changed, 70 insertions, 69 deletions
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 @@
<com:TContent ID="body" >
<!-- $Id $ -->
-<h1>Active Record</h1>
-<p>Active Records are objects that wrap a row in a database table or view,
+<h1 id="138046">Active Record</h1>
+<p id="690478" class="block-content">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
<tt>Products</tt> 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.
</div>
-<h2>When to Use It</h2>
-<p>Active Record is a good choice for domain logic that isn't too complex,
+<h2 id="138047">When to Use It</h2>
+<p id="690479" class="block-content">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.</p>
- <p>However, as your business logic grows in complexity, you'll soon want
+ <p id="690480" class="block-content">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.</p>
- <p>The alternative is to use a Data Mapper that separates the roles of the
+ <p id="690481" class="block-content">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
<a href="?page=Database.SqlMap">SqlMap Data Mapper</a>.
@@ -39,26 +39,28 @@
<img src=<%~ sqlmap_active_record.png %> alt="Active Records and SqlMap DataMapper" id="fig:diagram.png" class="figure"/>
</p>
- <p>
+ <p id="690482" class="block-content">
The Active Record class has methods that do the following:
- <ul>
+ </p>
+ <ul id="u1" class="block-content">
<li>Construct an instance of the Active Record from a SQL result set row.</li>
<li>Construct a new instance for later insertion into the table.</li>
<li>Finder methods to wrap commonly used SQL queries and return Active Record objects.</li>
<li>Update existing records and insert new records into the database.</li>
</ul>
- </p>
+<p id="p1" class="block-content">
The Active Record implementation utilizes the <a href="?page=Database.DAO">Prado DAO</a> classes for data access.
The current Active Record implementation supports
<a href="http://www.mysql.com">MySQL</a>,
<a href="http://www.postgres.com">Postgres SQL</a> and
<a href="http://www.sqlite.org">SQLite</a> databases.
Support for other databases can be provided when there are sufficient demand.
-<h2>Defining an Active Record</h2>
-<p>Let us
+</p>
+<h2 id="138048">Defining an Active Record</h2>
+<p id="690483" class="block-content">Let us
consider the following "users" table that contains two columns named "username" and "email",
where "username" is also the primary key.
-<com:TTextHighlighter Language="sql" CssClass="source">
+<com:TTextHighlighter Language="sql" CssClass="source block-content" id="code_690147">
CREATE TABLE users
(
username VARCHAR( 20 ) NOT NULL ,
@@ -67,8 +69,8 @@ CREATE TABLE users
);
</com:TTextHighlighter>
</p>
-<p>Next we define our Active Record class that corresponds to the "users" table.
-<com:TTextHighlighter Language="php" CssClass="source">
+<p id="690484" class="block-content">Next we define our Active Record class that corresponds to the "users" table.
+<com:TTextHighlighter Language="php" CssClass="source block-content" id="code_690148">
class UserRecord extends TActiveRecord
{
public $username; //the column named "username" in the "users" table
@@ -86,7 +88,7 @@ class UserRecord extends TActiveRecord
}
</com:TTextHighlighter>
</p>
-<p>Each property of the <tt>UserRecord</tt> class must correspond to a
+<p id="690485" class="block-content">Each property of the <tt>UserRecord</tt> class must correspond to a
column with the same name in the "users" table. The static class variable
<tt>$_tablename</tt> (must be public) is optional when the class name is the same as
the table name in the database, otherwise <tt>$_tablename</tt> must
@@ -102,7 +104,7 @@ E.g. MySQL uses back-ticks, <tt>$_tablename = "`database1`.`table1`"</tt>
Since <tt>TActiveRecord</tt> extends <tt>TComponent</tt>, setter and
getter methods can be defined to allow control over how variables
are set and returned. For example, adding a <tt>$level</tt> property to the UserRecord class:
-<com:TTextHighlighter Language="php" CssClass="source">
+<com:TTextHighlighter Language="php" CssClass="source block-content" id="code_690149">
class UserRecord extends TActiveRecord {
... //existing definitions as above
@@ -124,19 +126,19 @@ from views are read-only, calling the <tt>save()</tt> or <tt>delete()</tt> metho
will raise an exception.
</div>
-<p>
+<p id="690486" class="block-content">
The static method <tt>finder()</tt> returns an <tt>UserRecord</tt> 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 <tt>TActiveRecord::getRecordFinder()</tt>
static method takes the name of the current Active Record class as parameter.
</p>
-<h2>Setting up a database connection</h2>
-<p>
+<h2 id="138049">Setting up a database connection</h2>
+<p id="690487" class="block-content">
A default database connection for Active Record can be set as follows.
See <a href="?page=Database.DAO">Establishing Database Connection</a> for
futher details regarding creation of database connection in general.
-<com:TTextHighlighter Language="php" CssClass="source">
+<com:TTextHighlighter Language="php" CssClass="source block-content" id="code_690150">
//create a connection and give it to the ActiveRecord manager.
$dsn = 'pgsql:host=localhost;dbname=test'; //Postgres SQL
$conn = new TDbConnection($dsn, 'dbuser','dbpass');
@@ -144,11 +146,11 @@ TActiveRecordManager::getInstance()->setDbConnection($conn);
</com:TTextHighlighter>
</p>
-<p>
+<p id="690488" class="block-content">
The default database connection can also be configured using a <tt>&lt;module&gt;</tt>
tag in the <a href="?page=Configurations.AppConfig">application.xml</a>
or <a href="?page=Configurations.PageConfig">config.xml</a> as follows.
-<com:TTextHighlighter Language="xml" CssClass="source">
+<com:TTextHighlighter Language="xml" CssClass="source block-content" id="code_690151">
<modules>
<module class="System.Data.ActiveRecord.TActiveRecordConfig" EnableCache="true">
<database ConnectionString="pgsql:host=localhost;dbname=test"
@@ -165,10 +167,10 @@ TActiveRecordManager::getInstance()->setDbConnection($conn);
</div>
</p>
-<p>A <tt>ConnectionID</tt> property can be specified with value corresponding
+<p id="690489" class="block-content">A <tt>ConnectionID</tt> property can be specified with value corresponding
to another <tt>TDataSourceConfig</tt> module configuration's ID value. This allows
the same database connection to be used in other modules such as <a href="?page=Database.SqlMap">SqlMap</a>.
-<com:TTextHighlighter Language="xml" CssClass="source">
+<com:TTextHighlighter Language="xml" CssClass="source block-content" id="code_690152">
<modules>
<module class="System.Data.TDataSourceConfig" ID="db1">
<database ConnectionString="pgsql:host=localhost;dbname=test"
@@ -184,16 +186,16 @@ TActiveRecordManager::getInstance()->setDbConnection($conn);
</com:TTextHighlighter>
</p>
-<h2>Loading data from the database</h2>
-<p>
+<h2 id="138050">Loading data from the database</h2>
+<p id="690490" class="block-content">
The <tt>TActiveRecord</tt> class provides many convenient methods to find
records from the database. The simplest is finding records by matching primary keys.
See the <com:DocLink ClassPath="System.Data.ActiveRecord.TActiveRecord" /> for
more details.
</p>
- <h3><tt>findByPk()</tt></h3>
- <p>Finds one record using only the primary key or composite primary keys.
-<com:TTextHighlighter Language="php" CssClass="source">
+ <h3 id="138055"><tt>findByPk()</tt></h3>
+ <p id="690491" class="block-content">Finds one record using only the primary key or composite primary keys.
+<com:TTextHighlighter Language="php" CssClass="source block-content" id="code_690153">
$finder = UserRecord::finder();
$user = $finder->findByPk($primaryKey);
@@ -203,16 +205,16 @@ $record = $finder->findByPk(array($key1, $key2,...));
</com:TTextHighlighter>
</p>
- <h3><tt>findAllByPks()</tt></h3>
- <p>Finds multiple records using a list of primary keys or composite primary keys.
+ <h3 id="138056"><tt>findAllByPks()</tt></h3>
+ <p id="690492" class="block-content">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).
-<com:TTextHighlighter Language="php" CssClass="source">
+<com:TTextHighlighter Language="php" CssClass="source block-content" id="code_690154">
$finder = UserRecord::finder();
$users = $finder->findAllByPk($key1, $key2, ...);
$users = $finder->findAllByPk(array($key1, $key2, ...));
</com:TTextHighlighter>
The following are equivalent for composite keys.
-<com:TTextHighlighter Language="php" CssClass="source">
+<com:TTextHighlighter Language="php" CssClass="source block-content" id="code_690155">
//when the table uses composite keys
$record = $finder->findAllByPks(array($key1, $key2), array($key3, $key4), ...);
@@ -223,10 +225,10 @@ $record = $finder->findAllByPks($keys);
</p>
-<h3><tt>find()</tt></h3>
-<p>Finds <b>one single record</b> that matches the criteria. The criteria
- can be a partial SQL string or a <tt>TActiveRecordCriteria</tt> object.
-<com:TTextHighlighter Language="php" CssClass="source">
+<h3 id="138057"><tt>find()</tt></h3>
+<p id="690493" class="block-content">Finds <b>one single record</b> that matches the criteria. The criteria
+ can be a partial SQL string or a <tt>TActiveRecordCriteria</tt> object.</p>
+<com:TTextHighlighter Language="php" CssClass="source block-content" id="code_690156">
$finder = UserRecord::finder();
//:name and :pass are place holders for specific values of $name and $pass
@@ -241,19 +243,18 @@ $finder->find('username = ? AND password = ?', $name, $pass);
//$criteria is of TActiveRecordCriteria
$finder->find($criteria); //the 2nd parameter for find() is ignored.
</com:TTextHighlighter>
-</p>
-<p>The <tt>TActiveRecordCriteria</tt> class has the following properties:
- <ul>
+<p id="690494" class="block-content">The <tt>TActiveRecordCriteria</tt> class has the following properties:
+</p>
+ <ul id="u2" class="block-content">
<li><tt>Parameters</tt> -- name value parameter pairs.</li>
<li><tt>OrderBy</tt> -- column name and ordering pairs.</li>
<li><tt>Condition</tt> -- parts of the WHERE SQL conditions.</li>
<li><tt>Limit</tt> -- maximum number of records to return.</li>
<li><tt>Offset</tt> -- record offset in the table.</li>
</ul>
-</p>
-<com:TTextHighlighter Language="php" CssClass="source">
+<com:TTextHighlighter Language="php" CssClass="source block-content" id="code_690157">
$criteria = new TActiveRecordCriteria;
$criteria->Condition = 'username = :name AND password = :pass';
$criteria->Parameters[':name'] = 'admin';
@@ -264,45 +265,45 @@ $criteria->Limit = 10;
$criteria->Offset = 20;
</com:TTextHighlighter>
-<h3><tt>findAll()</tt></h3>
-<p>Same as <tt>find()</tt> but returns an array of objects.</p>
+<h3 id="138058"><tt>findAll()</tt></h3>
+<p id="690495" class="block-content">Same as <tt>find()</tt> but returns an array of objects.</p>
-<h3><tt>findBy*()</tt> and <tt>findAllBy*()</tt></h3>
-<p>Dynamic find method using parts of method name as search criteria.
+<h3 id="138059"><tt>findBy*()</tt> and <tt>findAllBy*()</tt></h3>
+<p id="690496" class="block-content">Dynamic find method using parts of method name as search criteria.
Method names starting with <tt>findBy</tt> return 1 record only.
Method names starting with <tt>findAllBy</tt> return an array of records.
The condition is taken as part of the method name after <tt>findBy</tt> or <tt>findAllBy</tt>.
The following blocks of code are equivalent:
-<com:TTextHighlighter Language="php" CssClass="source">
+<com:TTextHighlighter Language="php" CssClass="source block-content" id="code_690158">
$finder->findByName($name)
$finder->find('Name = ?', $name);
</com:TTextHighlighter>
-<com:TTextHighlighter Language="php" CssClass="source">
+<com:TTextHighlighter Language="php" CssClass="source block-content" id="code_690159">
$finder->findByUsernameAndPassword($name,$pass);
$finder->findBy_Username_And_Password($name,$pass);
$finder->find('Username = ? AND Password = ?', $name, $pass);
</com:TTextHighlighter>
-<com:TTextHighlighter Language="php" CssClass="source">
+<com:TTextHighlighter Language="php" CssClass="source block-content" id="code_690160">
$finder->findAllByAge($age);
$finder->findAll('Age = ?', $age);
</com:TTextHighlighter>
</p>
-<h3><tt>findBySql()</tt></h3>
-<p>Finds records using full SQL, returns corresponding array of record objects.</p>
+<h3 id="138060"><tt>findBySql()</tt></h3>
+<p id="690497" class="block-content">Finds records using full SQL, returns corresponding array of record objects.</p>
-<h3><tt>count()</tt></h3>
-<p>Find the number of matchings records.</p>
+<h3 id="138061"><tt>count()</tt></h3>
+<p id="690498" class="block-content">Find the number of matchings records.</p>
-<h2>Inserting and updating records</h2>
-<p>
+<h2 id="138051">Inserting and updating records</h2>
+<p id="690499" class="block-content">
Add a new record using TActiveRecord is very simple, just create a new Active
Record object and call the <tt>save()</tt> method. E.g.
-<com:TTextHighlighter Language="php" CssClass="source">
+<com:TTextHighlighter Language="php" CssClass="source block-content" id="code_690161">
$user1 = new UserRecord();
$user1->username = "admin"
$user1->email = "admin@example.com";
@@ -320,19 +321,19 @@ defined with "autoincrement", the Active Record objects will be updated with the
incremented values.</div>
</p>
-<p>
+<p id="690500" class="block-content">
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 <tt>save()</tt> method.
-<com:TTextHighlighter Language="php" CssClass="source">
+<com:TTextHighlighter Language="php" CssClass="source block-content" id="code_690162">
$user = UserRecord::finder()->findByName('admin');
$user->email="test@example.com"; //change property
$user->save(); //update it.
</com:TTextHighlighter>
</p>
-<p>
+<p id="690501" class="block-content">
Active Record objects have a simple life-cycle illustrated in the following diagram.
<img src=<%~ object_states.png %> 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 <tt>find*()</tt>
@@ -346,22 +347,22 @@ internal states are changed. Calling the <tt>delete()</tt> method on the object
ends the object life-cycle, no futher actions can be performed on the object.
</p>
-<h2>Deleting existing records</h2>
-<p>
+<h2 id="138052">Deleting existing records</h2>
+<p id="690502" class="block-content">
To delete an existing record that is already loaded, just call the <tt>delete()</tt> method.
You can also delete records in the database by primary keys without
loading any records using the <tt>deleteByPk()</tt> method.
For example, to delete one or records with tables having a scalar primary key.
-<com:TTextHighlighter Language="php" CssClass="source">
+<com:TTextHighlighter Language="php" CssClass="source block-content" id="code_690163">
$finder->deleteByPk($primaryKey); //delete 1 record
$finder->deleteByPk($key1,$key2,...); //delete multiple records
$finder->deleteByPk(array($key1,$key2,...)); //delete multiple records
</com:TTextHighlighter>
</p>
-<p>
+<p id="690503" class="block-content">
For composite primary keys (determined automatically from the table definitions):
-<com:TTextHighlighter Language="php" CssClass="source">
+<com:TTextHighlighter Language="php" CssClass="source block-content" id="code_690164">
$finder->deleteByPk(array($key1,$key2)); //delete 1 record
//delete multiple records
@@ -372,10 +373,10 @@ $finder->deleteByPk(array( array($key1,$key2), array($key3,$key4), .. ));
</com:TTextHighlighter>
</p>
-<h2>Transactions</h2>
-<p>All Active Record objects contains the property <tt>DbConnection</tt>
+<h2 id="138053">Transactions</h2>
+<p id="690504" class="block-content">All Active Record objects contains the property <tt>DbConnection</tt>
that can be used to obtain a transaction object.
-<com:TTextHighlighter Language="php" CssClass="source">
+<com:TTextHighlighter Language="php" CssClass="source block-content" id="code_690165">
$finder = UserRecord::finder();
$transaction = $finder->DbConnection->beginTransaction();
@@ -392,8 +393,8 @@ catch(Exception $e) // an exception is raised if a query fails
}
</com:TTextHighlighter>
-<h2>References</h2>
-<ul>
+<h2 id="138054">References</h2>
+<ul id="u3" class="block-content">
<li>Fowler et. al. <i>Patterns of Enterprise Application Architecture</i>,
Addison Wesley, 2002.</li>
</ul>