3 * TResultMap class file.
5 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
6 * @link https://github.com/pradosoft/prado
7 * @copyright Copyright © 2005-2016 The PRADO Group
8 * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
9 * @package System.Data.SqlMap.Configuration
13 * TResultMap corresponds to <resultMap> mapping tag.
15 * A TResultMap lets you control how data is extracted from the result of a
16 * query, and how the columns are mapped to object properties. A TResultMap
17 * can describe the column type, a null value replacement, and complex property
18 * mappings including Collections.
20 * The <resultMap> can contain any number of property mappings that map object
21 * properties to the columns of a result element. The property mappings are
22 * applied, and the columns are read, in the order that they are defined.
23 * Maintaining the element order ensures consistent results between different
24 * drivers and providers.
26 * The {@link Class setClass()} property must be a PHP class object or array instance.
28 * The optional {@link Extends setExtends()} attribute can be set to the ID of
29 * another <resultMap> upon which to base this <resultMap>. All properties of the
30 * "parent" <resultMap> will be included as part of this <resultMap>, and values
31 * from the "parent" <resultMap> are set before any values specified by this <resultMap>.
33 * @author Wei Zhuo <weizho[at]gmail[dot]com>
34 * @package System.Data.SqlMap.Configuration
37 class TResultMap extends TComponent
43 private $_discriminator;
44 private $_typeHandlers;
48 * Initialize the columns collection.
50 public function __construct()
52 $this->_columns=new TMap;
56 * @return string a unique identifier for the <resultMap>.
58 public function getID()
64 * @param string a unique identifier for the <resultMap>.
66 public function setID($value)
72 * @return string result class name.
74 public function getClass()
80 * @param string result class name.
82 public function setClass($value)
84 $this->_class = $value;
88 * @return TMap result columns.
90 public function getColumns()
92 return $this->_columns;
96 * @return string result map extends another result map.
98 public function getExtends()
100 return $this->_extends;
104 * @param string result map extends another result map.
106 public function setExtends($value)
108 $this->_extends = $value;
112 * @return string result map groups by.
114 public function getGroupBy()
116 return $this->_groupBy;
120 * @param string result map group by
122 public function setGroupBy($value)
124 $this->_groupBy = $value;
128 * @return TDiscriminator result class discriminator.
130 public function getDiscriminator()
132 return $this->_discriminator;
136 * @param TDiscriminator result class discriminator.
138 public function setDiscriminator(TDiscriminator $value)
140 $this->_discriminator = $value;
144 * Add a TResultProperty to result mapping.
145 * @param TResultProperty result property.
147 public function addResultProperty(TResultProperty $property)
149 $this->_columns[$property->getProperty()] = $property;
153 * Create a new instance of the class of this result map.
154 * @param TSqlMapTypeHandlerRegistry type handler registry.
155 * @return mixed new result object.
156 * @throws TSqlMapException
158 public function createInstanceOfResult($registry)
160 $handler = $registry->getTypeHandler($this->getClass());
164 return $handler->createNewInstance();
166 return $registry->createInstanceOf($this->getClass());
168 catch (TSqlMapException $e)
170 throw new TSqlMapException(
171 'sqlmap_unable_to_create_new_instance',
172 $this->getClass(), get_class($handler), $this->getID());
177 * Result sub-mappings using the discriminiator column.
178 * @param TSqlMapTypeHandlerRegistry type handler registry
179 * @param array row data.
180 * @return TResultMap result sub-map.
182 public function resolveSubMap($registry,$row)
185 if(($disc = $this->getDiscriminator())!==null)
187 $value = $disc->getMapping()->getPropertyValue($registry,$row);
188 $subMap = $disc->getSubMap((string)$value);
192 else if($subMap !== $this)
193 $subMap = $subMap->resolveSubMap($registry,$row);