]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/framework/Data/SqlMap/Configuration/TResultMap.php
baculum: New Baculum API and Baculum Web
[bacula/bacula] / gui / baculum / framework / Data / SqlMap / Configuration / TResultMap.php
1 <?php
2 /**
3  * TResultMap class file.
4  *
5  * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
6  * @link https://github.com/pradosoft/prado
7  * @copyright Copyright &copy; 2005-2016 The PRADO Group
8  * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
9  * @package System.Data.SqlMap.Configuration
10  */
11
12 /**
13  * TResultMap corresponds to <resultMap> mapping tag.
14  *
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.
19  *
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.
25  *
26  * The {@link Class setClass()} property must be a PHP class object or array instance.
27  *
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>.
32  *
33  * @author Wei Zhuo <weizho[at]gmail[dot]com>
34  * @package System.Data.SqlMap.Configuration
35  * @since 3.1
36  */
37 class TResultMap extends TComponent
38 {
39         private $_columns;
40         private $_class;
41         private $_extends;
42         private $_groupBy;
43         private $_discriminator;
44         private $_typeHandlers;
45         private $_ID;
46
47         /**
48          * Initialize the columns collection.
49          */
50         public function __construct()
51         {
52                 $this->_columns=new TMap;
53         }
54
55         /**
56          * @return string a unique identifier for the <resultMap>.
57          */
58         public function getID()
59         {
60                 return $this->_ID;
61         }
62
63         /**
64          * @param string a unique identifier for the <resultMap>.
65          */
66         public function setID($value)
67         {
68                 $this->_ID=$value;
69         }
70
71         /**
72          * @return string result class name.
73          */
74         public function getClass()
75         {
76                 return $this->_class;
77         }
78
79         /**
80          * @param string result class name.
81          */
82         public function setClass($value)
83         {
84                 $this->_class = $value;
85         }
86
87         /**
88          * @return TMap result columns.
89          */
90         public function getColumns()
91         {
92                 return $this->_columns;
93         }
94
95         /**
96          * @return string result map extends another result map.
97          */
98         public function getExtends()
99         {
100                 return $this->_extends;
101         }
102
103         /**
104          * @param string result map extends another result map.
105          */
106         public function setExtends($value)
107         {
108                 $this->_extends = $value;
109         }
110
111         /**
112          * @return string result map groups by.
113          */
114         public function getGroupBy()
115         {
116                 return $this->_groupBy;
117         }
118
119         /**
120          * @param string result map group by
121          */
122         public function setGroupBy($value)
123         {
124                 $this->_groupBy = $value;
125         }
126
127         /**
128          * @return TDiscriminator result class discriminator.
129          */
130         public function getDiscriminator()
131         {
132                 return $this->_discriminator;
133         }
134
135         /**
136          * @param TDiscriminator result class discriminator.
137          */
138         public function setDiscriminator(TDiscriminator $value)
139         {
140                 $this->_discriminator = $value;
141         }
142
143         /**
144          * Add a TResultProperty to result mapping.
145          * @param TResultProperty result property.
146          */
147         public function addResultProperty(TResultProperty $property)
148         {
149                 $this->_columns[$property->getProperty()] = $property;
150         }
151
152         /**
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
157          */
158         public function createInstanceOfResult($registry)
159         {
160                 $handler = $registry->getTypeHandler($this->getClass());
161                 try
162                 {
163                         if($handler!==null)
164                                 return $handler->createNewInstance();
165                         else
166                                 return $registry->createInstanceOf($this->getClass());
167                 }
168                 catch (TSqlMapException $e)
169                 {
170                         throw new TSqlMapException(
171                                 'sqlmap_unable_to_create_new_instance',
172                                         $this->getClass(), get_class($handler), $this->getID());
173                 }
174         }
175
176         /**
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.
181          */
182         public function resolveSubMap($registry,$row)
183         {
184                 $subMap = $this;
185                 if(($disc = $this->getDiscriminator())!==null)
186                 {
187                         $value = $disc->getMapping()->getPropertyValue($registry,$row);
188                         $subMap = $disc->getSubMap((string)$value);
189
190                         if($subMap===null)
191                                 $subMap = $this;
192                         else if($subMap !== $this)
193                                 $subMap = $subMap->resolveSubMap($registry,$row);
194                 }
195                 return $subMap;
196         }
197 }
198