| 1 |
lars |
1 |
<com:TContent ID="body">
|
|
|
2 |
<h1>Using SQLMap PHP DataMapper</h1>
|
|
|
3 |
<p>The SQLMap DataMapper API provides four core functions:</p>
|
|
|
4 |
<ol>
|
|
|
5 |
<li>build a <tt>TSqlMapper</tt> instance from a configuration file or cache</li>
|
|
|
6 |
<li>execute an update query (including insert and delete)</li>
|
|
|
7 |
<li>execute a select query for a single object</li>
|
|
|
8 |
<li>execute a select query for a list of objects</li>
|
|
|
9 |
</ol>
|
|
|
10 |
|
|
|
11 |
<p>The API also provides support for retrieving paginated lists and managing
|
|
|
12 |
transactions.</p>
|
|
|
13 |
|
|
|
14 |
<h1>Building a <tt>TSqlMapper</tt> instance</h1>
|
|
|
15 |
<p>An XML document is a wonderful tool for describing a database configuration
|
|
|
16 |
, but you can't execute XML. In order to use the
|
|
|
17 |
SQLMap configuration and definitions in your PHP application, you need a class
|
|
|
18 |
you can call.</p>
|
|
|
19 |
|
|
|
20 |
<p>The framework provides service methods that you can call which read the
|
|
|
21 |
configuration file (and any of its definition files) and builds a
|
|
|
22 |
<tt>TSqlMapper</tt> object. The <tt>TSqlMapper</tt> object provides access to the rest
|
|
|
23 |
of the framework. The following example shows a singleton <tt>TMapper</tt> that is
|
|
|
24 |
similar to the one bundled with the framework.</p>
|
|
|
25 |
|
|
|
26 |
<com:TTextHighlighter Language="php" CssClass="source">
|
|
|
27 |
require_once('/path/to/SQLMap/TSqlMapper.php');
|
|
|
28 |
class TMapper
|
|
|
29 |
{
|
|
|
30 |
private static $_mapper;
|
|
|
31 |
|
|
|
32 |
public static function configure($configFile)
|
|
|
33 |
{
|
|
|
34 |
if(is_null(self::$_mapper))
|
|
|
35 |
{
|
|
|
36 |
$builder = new TDomSqlMapBuilder();
|
|
|
37 |
self::$_mapper = $builder->configure($configFile);
|
|
|
38 |
}
|
|
|
39 |
return self::$_mapper;
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
public static function instance()
|
|
|
43 |
{
|
|
|
44 |
return self::$_mapper;
|
|
|
45 |
}
|
|
|
46 |
}
|
|
|
47 |
</com:TTextHighlighter>
|
|
|
48 |
|
|
|
49 |
<p>To obtain the <tt>TSqlMapper</tt> instance, first configure the mapper once.</p>
|
|
|
50 |
<com:TTextHighlighter Language="php" CssClass="source">
|
|
|
51 |
TMapper::configure('path/to/sqlmap.config');
|
|
|
52 |
</com:TTextHighlighter>
|
|
|
53 |
|
|
|
54 |
<p>The <tt>TDomSqlMapBuilder</tt> object will go throught the the <tt>sqlmap.config</tt>
|
|
|
55 |
file and build a <tt>TSqlMapper</tt> instance. To use <tt>TSqlMapper</tt> in your
|
|
|
56 |
application, specify one of the <tt>TSqlMapper</tt> methods. Here's an example:</p>
|
|
|
57 |
|
|
|
58 |
<com:TTextHighlighter Language="php" CssClass="source">
|
|
|
59 |
$list = TMapper::instance()->queryForList("PermitNoForYearList", $values);
|
|
|
60 |
</com:TTextHighlighter>
|
|
|
61 |
|
|
|
62 |
<h2>Multiple Databases</h2>
|
|
|
63 |
<p>If you need access to more than one database from the same application, create
|
|
|
64 |
a DataMapper configuration file for that database and another Mapper class to
|
|
|
65 |
go with it.</p>
|
|
|
66 |
|
|
|
67 |
<h2><tt>TDomSqlMapBuilder</tt> Configuration Options</h2>
|
|
|
68 |
<p>If you find that you already have loaded your DataMapper configuration
|
|
|
69 |
information as a <tt>SimpleXMLElement</tt> instance within your application, the
|
|
|
70 |
<tt>TDomSqlMapBuilder</tt> provides <tt>Configure</tt> overloads for those types as
|
|
|
71 |
well.</p>
|
|
|
72 |
|
|
|
73 |
</com:TContent>
|