Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * TResultMap class file.
4
 *
5
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
6
 * @link http://www.pradosoft.com/
7
 * @copyright Copyright &copy; 2005-2008 PradoSoft
8
 * @license http://www.pradosoft.com/license/
9
 * @version $Id: TResultMap.php 2541 2008-10-21 15:05:13Z qiang.xue $
10
 * @package System.Data.SqlMap.Configuration
11
 */
12
 
13
/**
14
 * TResultMap corresponds to <resultMap> mapping tag.
15
 *
16
 * A TResultMap lets you control how data is extracted from the result of a
17
 * query, and how the columns are mapped to object properties. A TResultMap
18
 * can describe the column type, a null value replacement, and complex property
19
 * mappings including Collections.
20
 *
21
 * The <resultMap> can contain any number of property mappings that map object
22
 * properties to the columns of a result element. The property mappings are
23
 * applied, and the columns are read, in the order that they are defined.
24
 * Maintaining the element order ensures consistent results between different
25
 * drivers and providers.
26
 *
27
 * The {@link Class setClass()} property must be a PHP class object or array instance.
28
 *
29
 * The optional {@link Extends setExtends()} attribute can be set to the ID of
30
 * another <resultMap> upon which to base this <resultMap>. All properties of the
31
 * "parent" <resultMap> will be included as part of this <resultMap>, and values
32
 * from the "parent" <resultMap> are set before any values specified by this <resultMap>.
33
 *
34
 * @author Wei Zhuo <weizho[at]gmail[dot]com>
35
 * @version $Id: TResultMap.php 2541 2008-10-21 15:05:13Z qiang.xue $
36
 * @package System.Data.SqlMap.Configuration
37
 * @since 3.1
38
 */
39
class TResultMap extends TComponent
40
{
41
	private $_columns;
42
	private $_class;
43
	private $_extends;
44
	private $_groupBy;
45
	private $_discriminator;
46
	private $_typeHandlers;
47
	private $_ID;
48
 
49
	/**
50
	 * Initialize the columns collection.
51
	 */
52
	public function __construct()
53
	{
54
		$this->_columns=new TMap;
55
	}
56
 
57
	/**
58
	 * @return string a unique identifier for the <resultMap>.
59
	 */
60
	public function getID()
61
	{
62
		return $this->_ID;
63
	}
64
 
65
	/**
66
	 * @param string a unique identifier for the <resultMap>.
67
	 */
68
	public function setID($value)
69
	{
70
		$this->_ID=$value;
71
	}
72
 
73
	/**
74
	 * @return string result class name.
75
	 */
76
	public function getClass()
77
	{
78
		return $this->_class;
79
	}
80
 
81
	/**
82
	 * @param string result class name.
83
	 */
84
	public function setClass($value)
85
	{
86
		$this->_class = $value;
87
	}
88
 
89
	/**
90
	 * @return TMap result columns.
91
	 */
92
	public function getColumns()
93
	{
94
		return $this->_columns;
95
	}
96
 
97
	/**
98
	 * @return string result map extends another result map.
99
	 */
100
	public function getExtends()
101
	{
102
		return $this->_extends;
103
	}
104
 
105
	/**
106
	 * @param string result map extends another result map.
107
	 */
108
	public function setExtends($value)
109
	{
110
		$this->_extends = $value;
111
	}
112
 
113
	/**
114
	 * @return string result map groups by.
115
	 */
116
	public function getGroupBy()
117
	{
118
		return $this->_groupBy;
119
	}
120
 
121
	/**
122
	 * @param string result map group by
123
	 */
124
	public function setGroupBy($value)
125
	{
126
		$this->_groupBy = $value;
127
	}
128
 
129
	/**
130
	 * @return TDiscriminator result class discriminator.
131
	 */
132
	public function getDiscriminator()
133
	{
134
		return $this->_discriminator;
135
	}
136
 
137
	/**
138
	 * @param TDiscriminator result class discriminator.
139
	 */
140
	public function setDiscriminator(TDiscriminator $value)
141
	{
142
		$this->_discriminator = $value;
143
	}
144
 
145
	/**
146
	 * Add a TResultProperty to result mapping.
147
	 * @param TResultProperty result property.
148
	 */
149
	public function addResultProperty(TResultProperty $property)
150
	{
151
		$this->_columns[$property->getProperty()] = $property;
152
	}
153
 
154
	/**
155
	 * Create a new instance of the class of this result map.
156
	 * @param TSqlMapTypeHandlerRegistry type handler registry.
157
	 * @return mixed new result object.
158
	 * @throws TSqlMapException
159
	 */
160
	public function createInstanceOfResult($registry)
161
	{
162
		$handler = $registry->getTypeHandler($this->getClass());
163
		try
164
		{
165
			if(!is_null($handler))
166
				return $handler->createNewInstance();
167
			else
168
				return $registry->createInstanceOf($this->getClass());
169
		}
170
		catch (TSqlMapException $e)
171
		{
172
			throw new TSqlMapException(
173
				'sqlmap_unable_to_create_new_instance',
174
					$this->getClass(), get_class($handler), $this->getID());
175
		}
176
	}
177
 
178
	/**
179
	 * Result sub-mappings using the discriminiator column.
180
	 * @param TSqlMapTypeHandlerRegistry type handler registry
181
	 * @param array row data.
182
	 * @return TResultMap result sub-map.
183
	 */
184
	public function resolveSubMap($registry,$row)
185
	{
186
		$subMap = $this;
187
		if(!is_null($disc = $this->getDiscriminator()))
188
		{
189
			$value = $disc->getMapping()->getPropertyValue($registry,$row);
190
			$subMap = $disc->getSubMap((string)$value);
191
 
192
			if(is_null($subMap))
193
				$subMap = $this;
194
			else if($subMap !== $this)
195
				$subMap = $subMap->resolveSubMap($registry,$row);
196
		}
197
		return $subMap;
198
	}
199
}
200