| 1 |
lars |
1 |
<com:TContent ID="body">
|
|
|
2 |
|
|
|
3 |
<h1>Custom Type Handlers</h1>
|
|
|
4 |
<p>A custom type handler allows you to extend the DataMapper's capabilities in
|
|
|
5 |
handling types that are specific to your database provider, not handled by
|
|
|
6 |
your database provider, or just happen to be part of your application design.
|
|
|
7 |
The SQLMap for PHP DataMapper provides an interface,
|
|
|
8 |
<tt>ITypeHandlerCallback</tt>, for you to use in implementing your custom type
|
|
|
9 |
handler.</p>
|
|
|
10 |
|
|
|
11 |
<com:TTextHighlighter Language="php" CssClass="source">
|
|
|
12 |
interface ITypeHandlerCallback
|
|
|
13 |
{
|
|
|
14 |
public function getParameter($object);
|
|
|
15 |
|
|
|
16 |
public function getResult($string);
|
|
|
17 |
|
|
|
18 |
public function createNewInstance();
|
|
|
19 |
}
|
|
|
20 |
</com:TTextHighlighter>
|
|
|
21 |
|
|
|
22 |
<p>The <tt>getParameter</tt> method allows you to process a <tt><statement></tt>
|
|
|
23 |
parameter's value before it is added as an parameter. This enables you to do
|
|
|
24 |
any necessary type conversion and clean-up before the DataMapper gets to work.</p>
|
|
|
25 |
|
|
|
26 |
<p>The <tt>getResult</tt> method allows you to process a database result value right
|
|
|
27 |
after it has been retrieved by the DataMapper and before it is used in your
|
|
|
28 |
<tt>resultClass</tt>, <tt>resultMap</tt>, or <tt>listClass</tt>.</p>
|
|
|
29 |
|
|
|
30 |
<p>The <tt>createNewInstance</tt> method allows the DataMapper to create new instance
|
|
|
31 |
of a particular type handled by this callback.</p>
|
|
|
32 |
|
|
|
33 |
<p>One scenario where custom type handlers are useful are the when you want to
|
|
|
34 |
use date time values in the database. First, consider a very basic TDateTime
|
|
|
35 |
class.</p>
|
|
|
36 |
|
|
|
37 |
<com:TTextHighlighter Language="php" CssClass="source">
|
|
|
38 |
class TDateTime
|
|
|
39 |
{
|
|
|
40 |
private $_datetime;
|
|
|
41 |
|
|
|
42 |
public function __construct($datetime=null)
|
|
|
43 |
{
|
|
|
44 |
if(!is_null($datetime))
|
|
|
45 |
$this->setDatetime($datetime);
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
public function getTimestamp()
|
|
|
49 |
{
|
|
|
50 |
return strtotime($this->getDatetime());
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
public function getDateTime()
|
|
|
54 |
{
|
|
|
55 |
return $this->_datetime;
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
public function setDateTime($value)
|
|
|
59 |
{
|
|
|
60 |
$this->_datetime = $value;
|
|
|
61 |
}
|
|
|
62 |
}
|
|
|
63 |
</com:TTextHighlighter>
|
|
|
64 |
|
|
|
65 |
<p>We can use a custom type handler to intercept result and parameter mapping
|
|
|
66 |
that uses the say "data" as one of its property type. The handler can be
|
|
|
67 |
written as follows.</p>
|
|
|
68 |
|
|
|
69 |
<com:TTextHighlighter Language="php" CssClass="source">
|
|
|
70 |
class TDateTimeHandler implements ITypeHandlerCallback
|
|
|
71 |
{
|
|
|
72 |
public function getResult($string)
|
|
|
73 |
{
|
|
|
74 |
return new TDateTime($string);
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
public function getParameter($parameter)
|
|
|
78 |
{
|
|
|
79 |
if($parameter instanceof TDateTime)
|
|
|
80 |
return $parameter->getTimestamp();
|
|
|
81 |
else
|
|
|
82 |
return $parameter;
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
public function createNewInstance()
|
|
|
86 |
{
|
|
|
87 |
return new TDateTime;
|
|
|
88 |
}
|
|
|
89 |
}
|
|
|
90 |
</com:TTextHighlighter>
|
|
|
91 |
|
|
|
92 |
<p>With our custom type handler we can use the handler in our SqlMaps. To do
|
|
|
93 |
that, we specify it as a basic <tt><typeHandler></tt> for all <tt>date</tt> types
|
|
|
94 |
mapped in our SqlMap files</p>
|
|
|
95 |
|
|
|
96 |
<com:TTextHighlighter Language="xml" CssClass="source">
|
|
|
97 |
[Our SqlMap.config]
|
|
|
98 |
|
|
|
99 |
<typeHandlers>
|
|
|
100 |
<typeHandler type="date" callback="TDateTimeHandler"/>
|
|
|
101 |
</typeHandlers>
|
|
|
102 |
|
|
|
103 |
|
|
|
104 |
[One of our SqlMap.xml files]
|
|
|
105 |
<parameterMap id="boc-params">
|
|
|
106 |
<parameter property="releasedDate" type="date"/>
|
|
|
107 |
</parameterMap>
|
|
|
108 |
|
|
|
109 |
<resultMap id="boc-result" class="BudgetObjectCode">
|
|
|
110 |
<result property="releasedDate" column="BOC_DATE" type="date"/>
|
|
|
111 |
</resultMap>
|
|
|
112 |
</com:TTextHighlighter>
|
|
|
113 |
|
|
|
114 |
</com:TContent>
|