| 1 |
lars |
1 |
<com:TContent ID="body">
|
|
|
2 |
|
|
|
3 |
<h1>Exploring the SQLMap PHP DataMapper API through the <tt>TSqlMapper</tt></h1>
|
|
|
4 |
<p>The <tt>TSqlMapper</tt> instance acts as a facade to provide access the rest of
|
|
|
5 |
the DataMapper framework. The DataMapper API methods are shown below.</p>
|
|
|
6 |
|
|
|
7 |
<com:TTextHighlighter Language="php" CssClass="source">
|
|
|
8 |
/* Query API */
|
|
|
9 |
public function queryForObject($statementName, $parameter=null, $result=null);
|
|
|
10 |
public function queryForList($statementName, $parameter=null, $result=null,
|
|
|
11 |
$skip=-1, $max=-1);
|
|
|
12 |
public function queryForPagedList($statementName, $parameter=null, $pageSize=10, $page=0);
|
|
|
13 |
public function queryForMap($statementName, $parameter=null,
|
|
|
14 |
$keyProperty=null, $valueProperty=null);
|
|
|
15 |
public function insert($statementName, $parameter=null)
|
|
|
16 |
public function update($statementName, $parameter=null)
|
|
|
17 |
public function delete($statementName, $parameter=null)
|
|
|
18 |
|
|
|
19 |
/* Connection API */
|
|
|
20 |
public function openConnection()
|
|
|
21 |
public function closeConnection()
|
|
|
22 |
|
|
|
23 |
/* Transaction API */
|
|
|
24 |
public function beginTransaction()
|
|
|
25 |
public function commitTransaction()
|
|
|
26 |
public function rollBackTransaction()
|
|
|
27 |
</com:TTextHighlighter>
|
|
|
28 |
|
|
|
29 |
<p>Note that each of the API methods accept the name of the Mapped Statement as
|
|
|
30 |
the first parameter. The <tt>statementName</tt> parameter corresponds to the
|
|
|
31 |
<tt>id</tt> of the Mapped Statement in the Data Map definition.
|
|
|
32 |
In each case, a <tt>parameterObject</tt> also may be
|
|
|
33 |
passed. The following sections describe how the API methods work.</p>
|
|
|
34 |
|
|
|
35 |
<h2>Insert, Update, Delete</h2>
|
|
|
36 |
<com:TTextHighlighter Language="php" CssClass="source">
|
|
|
37 |
public function insert($statementName, $parameter=null)
|
|
|
38 |
public function update($statementName, $parameter=null)
|
|
|
39 |
public function delete($statementName, $parameter=null)
|
|
|
40 |
</com:TTextHighlighter>
|
|
|
41 |
|
|
|
42 |
<p>If a Mapped Statement uses one of the <tt><insert></tt>, <tt><update></tt>, or
|
|
|
43 |
<tt><delete></tt> statement-types, then it should use the corresponding API
|
|
|
44 |
method. The <tt><insert></tt> element supports a nested <tt><selectKey></tt> element
|
|
|
45 |
for generating primary keys. If the
|
|
|
46 |
<tt><selectKey></tt> stanza is used, then <tt>insert</tt> returns the generated key;
|
|
|
47 |
otherwise a null object is returned. Both the <tt>update</tt> and <tt>delete</tt>
|
|
|
48 |
methods return the number of rows affected by the statement.
|
|
|
49 |
</p>
|
|
|
50 |
|
|
|
51 |
<h2>QueryForObject</h2>
|
|
|
52 |
<com:TTextHighlighter Language="php" CssClass="source">
|
|
|
53 |
public function queryForObject($statementName, $parameter=null, $result=null);
|
|
|
54 |
</com:TTextHighlighter>
|
|
|
55 |
|
|
|
56 |
<p>If a Mapped Statement is expected to select a single row, then call it using
|
|
|
57 |
<tt>queryForObject</tt>. Since the Mapped Statement definition specifies the
|
|
|
58 |
result class expected, the framework can both create and populate the result
|
|
|
59 |
class for you. Alternatively, if you need to manage the result object
|
|
|
60 |
yourself, say because it is being populated by more than one statement, you
|
|
|
61 |
can use the alternate form and pass your <tt>$resultObject</tt> as the third
|
|
|
62 |
parameter.</p>
|
|
|
63 |
|
|
|
64 |
<h2>QueryForList</h2>
|
|
|
65 |
|
|
|
66 |
<com:TTextHighlighter Language="php" CssClass="source">
|
|
|
67 |
public function queryForList($statementName, $parameter=null, $result=null,
|
|
|
68 |
$skip=-1, $max=-1);
|
|
|
69 |
</com:TTextHighlighter>
|
|
|
70 |
|
|
|
71 |
<p>If a Mapped Statement is expected to select multiple rows, then call it using
|
|
|
72 |
<tt>queryForList</tt>. Each entry in the list will be an result object populated
|
|
|
73 |
from the corresponding row of the query result. If you need to manage the
|
|
|
74 |
<tt>$resultObject</tt> yourself, then it can be passed as the third parameter. If
|
|
|
75 |
you need to obtain a partial result, the fourth parameter <tt>$skip</tt> and
|
|
|
76 |
fifth parameter <tt>$max</tt> allow you to skip a number of records (the starting
|
|
|
77 |
point) and the maximum number to return.</p>
|
|
|
78 |
|
|
|
79 |
<h2>QueryForPagedList</h2>
|
|
|
80 |
<com:TTextHighlighter Language="php" CssClass="source">
|
|
|
81 |
public function queryForPagedList($statementName, $parameter=null, $pageSize=10, $page);
|
|
|
82 |
</com:TTextHighlighter>
|
|
|
83 |
|
|
|
84 |
<p>We live in an age of information overflow. A database query often returns more
|
|
|
85 |
hits than users want to see at once, and our requirements may say that we need
|
|
|
86 |
to offer a long list of results a "page" at a time. If the query returns
|
|
|
87 |
1000 hits, we might need to present the hits to the user in sets of fifty, and
|
|
|
88 |
let them move back and forth between the sets. Since this is such a common
|
|
|
89 |
requirement, the framework provides a convenience method.</p>
|
|
|
90 |
|
|
|
91 |
<p>The <tt>TSqlMapPagedList</tt> interface includes methods for navigating through
|
|
|
92 |
pages (<tt>nextPage()</tt>, <tt>previousPage()</tt>, <tt>gotoPage($pageIndex)</tt>) and
|
|
|
93 |
also checking the status of the page (<tt>getIsFirstPage()</tt>,
|
|
|
94 |
<tt>getIsMiddlePage()</tt>, <tt>getIsLastPage()</tt>, <tt>getIsNextPageAvailable()</tt>,
|
|
|
95 |
<tt>getIsPreviousPageAvailable()</tt>, <tt>getCurrentPageIndex()</tt>,
|
|
|
96 |
<tt>getPageSize()</tt>). The total number of records available is not accessible
|
|
|
97 |
from the <tt>TSqlMapPagedList</tt> interface, unless a virtual count is defined
|
|
|
98 |
using <tt>setVirtualCount($value)</tt>, this should be easily accomplished by
|
|
|
99 |
simply executing a second statement that counts the expected results.</p>
|
|
|
100 |
|
|
|
101 |
<div class="tip"><b class="tip">Tip:</b>
|
|
|
102 |
The <tt>queryForPagedList</tt> method is convenient, but note that a larger set
|
|
|
103 |
(up to 3 times the page size) will first be returned by the database provider
|
|
|
104 |
and the smaller set extracted by the framework. The higher the page size, the
|
|
|
105 |
larger set that will be returned and thrown away. For very large sets, you may
|
|
|
106 |
want to use a stored procedure or your own query that uses <tt>$skip</tt> and
|
|
|
107 |
<tt>$max</tt> as parameters in <tt>queryForList</tt>.
|
|
|
108 |
</div>
|
|
|
109 |
|
|
|
110 |
<div class="tip"><b class="tip">Tip:</b>
|
|
|
111 |
The <tt>$page</tt> parameter was introduced in 3.1.3. Before there was an additional
|
|
|
112 |
query to always fetch the data for page 0 on object creation. Since this
|
|
|
113 |
might be a problem in performance critical situations with 3.1.2, you might be better
|
|
|
114 |
of also using <tt>queryForList</tt> with <tt>$skip</tt> and <tt>$max</tt> instead.
|
|
|
115 |
</div>
|
|
|
116 |
|
|
|
117 |
|
|
|
118 |
<h2>QueryForMap</h2>
|
|
|
119 |
<com:TTextHighlighter Language="php" CssClass="source">
|
|
|
120 |
public function queryForMap($statementName, $parameter=null,
|
|
|
121 |
$keyProperty=null, $valueProperty=null);
|
|
|
122 |
</com:TTextHighlighter>
|
|
|
123 |
|
|
|
124 |
<p>The <tt>queryForList</tt> methods return the result objects within a <tt>TList</tt> or
|
|
|
125 |
array instance. Alternatively, the <tt>queryForMap</tt> returns a TMap or
|
|
|
126 |
associative array instance. The value of each entry is one of the result
|
|
|
127 |
objects. The key to each entry is indicated by the <tt>$keyProperty</tt>
|
|
|
128 |
parameter. This is the name of the one of the properties of the result object,
|
|
|
129 |
the value of which is used as the key for each entry. For example, If you
|
|
|
130 |
needed a set of <tt>Employee</tt> objects, you might want them returned as a
|
|
|
131 |
<tt>TMap</tt> keyed by each object's <tt>EmployeeNumber</tt> property.</p>
|
|
|
132 |
|
|
|
133 |
<p>If you don't need the entire result object in your result, you can add the
|
|
|
134 |
<tt>$valueProperty</tt> parameter to indicate which result object property should
|
|
|
135 |
be the value of an entry. For example, you might just want the
|
|
|
136 |
<tt>EmployeeName</tt> keyed by <tt>EmployeeNumber</tt>.</p>
|
|
|
137 |
|
|
|
138 |
<h2>Transaction</h2>
|
|
|
139 |
<p>The DataMapper API includes methods to demarcate transactional boundaries. A
|
|
|
140 |
transaction can be started, committed and/or rolled back. You can call the
|
|
|
141 |
transaction methods from the <tt>TSqlMapper</tt> instance.</p>
|
|
|
142 |
|
|
|
143 |
<com:TTextHighlighter Language="php" CssClass="source">
|
|
|
144 |
// Begin a transactional session using Adodb transaction API
|
|
|
145 |
public function beginTransaction()
|
|
|
146 |
|
|
|
147 |
// Commit a transaction, uses Adodb transaction API
|
|
|
148 |
public function commitTransaction()
|
|
|
149 |
|
|
|
150 |
// RollBack a transaction, uses Adodb transaction API
|
|
|
151 |
public void RollBackTransaction()
|
|
|
152 |
</com:TTextHighlighter>
|
|
|
153 |
|
|
|
154 |
<p>Using transactions example.</p>
|
|
|
155 |
<com:TTextHighlighter Language="php" CssClass="source">
|
|
|
156 |
try
|
|
|
157 |
{
|
|
|
158 |
$sqlMap->beginTransaction();
|
|
|
159 |
$item = $sqlMap->queryForObject("getItem", $itemId);
|
|
|
160 |
$item->setDescription($newDescription);
|
|
|
161 |
$sqlMap->update("updateItem", $item);
|
|
|
162 |
$sqlMap->commitTransaction();
|
|
|
163 |
}
|
|
|
164 |
catch
|
|
|
165 |
{
|
|
|
166 |
$sqlMap->rollBackTransaction();
|
|
|
167 |
}
|
|
|
168 |
</com:TTextHighlighter>
|
|
|
169 |
|
|
|
170 |
</com:TContent>
|