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/SqlMap.page | 84 +++++++++++----------- 1 file changed, 42 insertions(+), 42 deletions(-) (limited to 'demos/quickstart/protected/pages/Database/SqlMap.page') diff --git a/demos/quickstart/protected/pages/Database/SqlMap.page b/demos/quickstart/protected/pages/Database/SqlMap.page index 4b462168..bac1ba44 100644 --- a/demos/quickstart/protected/pages/Database/SqlMap.page +++ b/demos/quickstart/protected/pages/Database/SqlMap.page @@ -1,8 +1,8 @@ -

Data Mapper

-

Data Mappers moves data between objects and a database while keeping them +

Data Mapper

+

Data Mappers moves data between objects and a database while keeping them independent of each other and the mapper itself. If you started with Active Records, you may eventually faced with more complex business @@ -12,28 +12,28 @@ that is, the object schema and the relational schema don't match up.

-

The Data Mapper separates the in-memory objects from the database. Its responsibility +

The Data Mapper separates the in-memory objects from the database. Its responsibility is to transfer data between the two and also to isolate them from each other. With Data Mapper the in-memory objects needn't know even that there's a database present; they need no SQL interface code, and certainly no knowledge of the database schema. (The database schema is always ignorant of the objects that use it.)

-

When to Use It

-

The primary occasion for using Data Mapper is when you want the database schema +

When to Use It

+

The primary occasion for using Data Mapper is when you want the database schema and the object model to evolve independently. Data Mapper's primary benefit is that when working on the business (or domain) objects you can ignore the database, both in design and in the build and testing process. The domain objects have no idea what the database structure is, because all the correspondence is done by the mappers.

-

This helps you in the code because you can understand and work with the domain objects +

This helps you in the code because you can understand and work with the domain objects without having to understand how they're stored in the database. You can modify the business models or the database without having to alter either. With complicated mappings, particularly those involving existing databases, this is very valuable.

-

The price, of course, is the extra layer that you don't get with +

The price, of course, is the extra layer that you don't get with Active Record, so the test for using these patterns is the complexity of the business logic. If you have fairly simple business logic, an Active Record @@ -41,8 +41,8 @@ For more complicated logic a Data Mapper may be more suitable.

-

SqlMap Data Mapper

-

The SqlMap DataMapper framework makes it easier to use a database with a PHP application. +

SqlMap Data Mapper

+

The SqlMap DataMapper framework makes it easier to use a database with a PHP application. SqlMap DataMapper couples objects with stored procedures or SQL statements using a XML descriptor. Simplicity is the biggest advantage of the SqlMap DataMapper over object relational mapping tools. To use SqlMap DataMapper you rely on your own objects, @@ -51,7 +51,7 @@ your fingertip

-

+

alt="SqlMap Data Mapper Overview" id="fig:sqlmap.png" class="figure"/> Here's a high level description of the work flow illustrated in the figure abov. @@ -59,23 +59,23 @@ used to set runtime values in your SQL statement or stored procedure. If a runtime value is not needed, the parameter can be omitted.

-

Execute the mapping by passing the parameter and the name you gave the statement or +

Execute the mapping by passing the parameter and the name you gave the statement or procedure in your XML descriptor. This step is where the magic happens. The framework will prepare the SQL statement or stored procedure, set any runtime values using your parameter, execute the procedure or statement, and return the result.

-

In the case of an update, the number of rows affected is returned. In the case of a +

In the case of an update, the number of rows affected is returned. In the case of a query, a single object, or a collection of objects is returned. Like the parameter, the result object, or collection of objects, can be a plain-old object or a primitive PHP type.

-

Setting up a database connection and initializing the SqlMap

-

+

Setting up a database connection and initializing the SqlMap

+

A database connection for SqlMap can be set as follows. See Establishing Database Connection for futher details regarding creation of database connection in general. - + //create a connection and give it to the SqlMap manager. $dsn = 'pgsql:host=localhost;dbname=test'; //Postgres SQL $conn = new TDbConnection($dsn, 'dbuser','dbpass'); @@ -85,7 +85,7 @@ $sqlmap = $manager->getSqlMapGateway();

-

+

The TSqlMapManager is responsible for setting up the database connection and configuring the SqlMap with given XML file(s). The configureXml() method accepts a string that points to a SqlMap XML configuration file. Once @@ -93,11 +93,11 @@ $sqlmap = $manager->getSqlMapGateway(); of the SqlMap gateway interface (use this object to insert/delete/find records).

-

+

SqlMap database connection can also be configured using a <module> tag in the application.xml or config.xml as follows. - + @@ -108,7 +108,7 @@ $sqlmap = $manager->getSqlMapGateway();

-

+

The ConfigFile attribute should point to a SqlMap configuration file (to be detailed later) either using absolute path, relative path or the Prado's namespace dot notation path (must omit the ".xml" extension). @@ -122,9 +122,9 @@ $sqlmap = $manager->getSqlMapGateway();

-

To obtain the SqlMap gateway interface from the <module> configuration, simply +

To obtain the SqlMap gateway interface from the <module> configuration, simply do, for example, - + class MyPage extends TPage { public function onLoad($param) @@ -137,11 +137,11 @@ class MyPage extends TPage

-

A quick example

-

Let us +

A quick example

+

Let us consider the following "users" table that contains two columns named "username" and "email", where "username" is also the primary key. - + CREATE TABLE users ( username VARCHAR( 20 ) NOT NULL , @@ -150,9 +150,9 @@ CREATE TABLE users );

-

Next we define our plain User class as follows. Notice that +

Next we define our plain User class as follows. Notice that the User is very simple. - + class User { public $username; @@ -162,9 +162,9 @@ class User

-

Next, we need to define a SqlMap XMl configuration file, lets name +

Next, we need to define a SqlMap XMl configuration file, lets name the file as my-sqlmap.xml - + @@ -242,10 +242,10 @@ class UserRecord extends TActiveRecord

-

The PHP code for retrieving the users remains the same, but SqlMap +

The PHP code for retrieving the users remains the same, but SqlMap returns Active Records instead, and we can take advantage of the Active Record methods. - + //assume that $sqlmap is an TSqlMapGateway instance $user = $sqlmap->queryForObject("SelectUsers"); @@ -254,8 +254,8 @@ $user->save(); //save it using Active Record

-

References

-