summaryrefslogtreecommitdiff
path: root/demos/sqlmap/protected/pages/Manual/BuildingTSqlMapper.page
blob: 87165da295e4c85f3aa78a13ddf3075875f38232 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<com:TContent ID="body">
<h1>Using SQLMap PHP DataMapper</h1>
<p>The SQLMap DataMapper API provides four core functions:</p>
<ol>
  <li>build a <tt>TSqlMapper</tt> instance from a configuration file or cache</li>
  <li>execute an update query (including insert and delete)</li>
  <li>execute a select query for a single object</li>
  <li>execute a select query for a list of objects</li>
</ol>

<p>The API also provides support for retrieving paginated lists and managing
transactions.</p>

<h1>Building a <tt>TSqlMapper</tt> instance</h1>
<p>An XML document is a wonderful tool for describing a database configuration
, but you can't execute XML. In order to use the
SQLMap configuration and definitions in your PHP application, you need a class
you can call.</p>

<p>The framework provides service methods that you can call which read the
configuration file (and any of its definition files) and builds a
<tt>TSqlMapper</tt> object. The <tt>TSqlMapper</tt> object provides access to the rest
of the framework. The following example shows a singleton <tt>TMapper</tt> that is
similar to the one bundled with the framework.</p>

<com:TTextHighlighter Language="php" CssClass="source">
require_once('/path/to/SQLMap/TSqlMapper.php');
class TMapper
{
    private static $_mapper;

    public static function configure($configFile)
    {
        if(is_null(self::$_mapper))
        {
            $builder = new TDomSqlMapBuilder();
            self::$_mapper = $builder->configure($configFile);
        }
        return self::$_mapper;
    }

    public static function instance()
    {
        return self::$_mapper;
    }
}
</com:TTextHighlighter>

<p>To obtain the <tt>TSqlMapper</tt> instance, first configure the mapper once.</p>
<com:TTextHighlighter Language="php" CssClass="source">
TMapper::configure('path/to/sqlmap.config');
</com:TTextHighlighter>

<p>The <tt>TDomSqlMapBuilder</tt> object will go throught the the <tt>sqlmap.config</tt>
file and build a <tt>TSqlMapper</tt> instance. To use <tt>TSqlMapper</tt> in your
application, specify one of the <tt>TSqlMapper</tt> methods. Here's an example:</p>

<com:TTextHighlighter Language="php" CssClass="source">
$list = TMapper::instance()->queryForList("PermitNoForYearList", $values);
</com:TTextHighlighter>

<h2>Multiple Databases</h2>
<p>If you need access to more than one database from the same application, create
a DataMapper configuration file for that database and another Mapper class to
go with it.</p>

<h2><tt>TDomSqlMapBuilder</tt> Configuration Options</h2>
<p>If you find that you already have loaded your DataMapper configuration
information as a <tt>SimpleXMLElement</tt> instance within your application, the
<tt>TDomSqlMapBuilder</tt> provides <tt>Configure</tt> overloads for those types as
well.</p>

</com:TContent>