concept Data Mapper in category php

appears as: Data Mapper, Data Mapper, A Data Mapper
PHP in Action: Objects, Design, Agility

This is an excerpt from Manning's book PHP in Action: Objects, Design, Agility.

The DiscussionView class takes a discussion ID as input and does all the work of getting the data from the database and putting it into a form that is easy to display in a template. It might be tidier to give it the data instead, so that the View object doesn’t depend on the database-related code.
To get the data from the database, we use a Data Mapper called DiscussionMapper. Although we haven’t introduced Data Mappers yet, to understand this example, you just need to know that it’s a class that can be used for getting data from the database.
The mapper’s find() method takes the discussion ID and retrieves the discussion from the database. The discussion is an object-oriented tree structure composed of discussion nodes.
Now we call the method that converts the Composite structure into a simple list in the form of an array.
We remove the first element of the list. It’s the root node representing the entire discussion, and we don’t want that to show up on the web page. We just want the individual threads, which are the children of the root node.
The getList() method returns the contents of the discussion as an array in the order the posts will be listed on the web page. This is where the recursion happens.
We have a $depth variable to keep track of the current level in the hierarchy. When the method is called initially (on the root node), $depth is set to -1 and then incremented. So the root node’s depth is 0. Then, when we call the method on the children of the root node, we pass $depth on and it gets incremented to 1. And so it keeps increasing as we move recursively to deeper levels. An inelegant but relatively flexible way to use this is to generate separate CSS classes for each level (level1, level2, and so on). Assuming a limited number of levels, they can be separately styled in this manner: table#AdminList tr.level2 td.threaded { padding-left: 2em; }
table#AdminList tr.level3 td.threaded { padding-left: 4em; }
table#AdminList tr.level4 td.threaded { padding-left: 6em; }
table#AdminList tr.level5 td.threaded { padding-left: 8em; }
$array is an associative array representing a single node. The asArray() method converts the node from an object, potentially with children, to a plain associative array.
$result is the list that will contain this node and all its descendants. We build the list starting with the current node.
Each child generates a list of nodes, and we append the list to the result list.
Since a plain array is relatively easy to use with any template engine, generating an array from an object, as we’re doing here, may be reasonable. However, if we can, it might be better to use the object directly or via a decorator. An object-oriented data structure is more flexible and easier to modify.

In Fowler’s terminology, a Data Mapper is an object that gets objects from and stores objects to a database. The pattern differs from Active Record in using a completely separate mapper object to do the job, rather than the domain object itself.

The J2EE pattern Data Access Object [Alur et al.] is similar in principle, although the description of the pattern is concerned with retrieving and storing so-called Transfer Objects rather than real domain objects. But DAO is frequently used as a more general term encompassing more data access strategies than Data Mapper.

A Data Mapper is a Table Data Gateway that deconstructs an object

The Table Data Gateway and Data Mapper patterns are also very similar. The Table Data Gateway accepts the data as single values; the Data Mapper accepts it in the form of objects. In a simple Table Data Gateway, the insert() method may have this signature:

sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest