ResultSetMapping.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\ORM\Query;
  20. /**
  21. * A ResultSetMapping describes how a result set of an SQL query maps to a Doctrine result.
  22. *
  23. * IMPORTANT NOTE:
  24. * The properties of this class are only public for fast internal READ access and to (drastically)
  25. * reduce the size of serialized instances for more effective caching due to better (un-)serialization
  26. * performance.
  27. *
  28. * <b>Users should use the public methods.</b>
  29. *
  30. * @author Roman Borschel <roman@code-factory.org>
  31. * @since 2.0
  32. * @todo Think about whether the number of lookup maps can be reduced.
  33. */
  34. class ResultSetMapping
  35. {
  36. /**
  37. * Whether the result is mixed (contains scalar values together with field values).
  38. *
  39. * @ignore
  40. * @var boolean
  41. */
  42. public $isMixed = false;
  43. /**
  44. * Maps alias names to class names.
  45. *
  46. * @ignore
  47. * @var array
  48. */
  49. public $aliasMap = array();
  50. /**
  51. * Maps alias names to related association field names.
  52. *
  53. * @ignore
  54. * @var array
  55. */
  56. public $relationMap = array();
  57. /**
  58. * Maps alias names to parent alias names.
  59. *
  60. * @ignore
  61. * @var array
  62. */
  63. public $parentAliasMap = array();
  64. /**
  65. * Maps column names in the result set to field names for each class.
  66. *
  67. * @ignore
  68. * @var array
  69. */
  70. public $fieldMappings = array();
  71. /**
  72. * Maps column names in the result set to the alias/field name to use in the mapped result.
  73. *
  74. * @ignore
  75. * @var array
  76. */
  77. public $scalarMappings = array();
  78. /**
  79. * Maps column names of meta columns (foreign keys, discriminator columns, ...) to field names.
  80. *
  81. * @ignore
  82. * @var array
  83. */
  84. public $metaMappings = array();
  85. /**
  86. * Maps column names in the result set to the alias they belong to.
  87. *
  88. * @ignore
  89. * @var array
  90. */
  91. public $columnOwnerMap = array();
  92. /**
  93. * List of columns in the result set that are used as discriminator columns.
  94. *
  95. * @ignore
  96. * @var array
  97. */
  98. public $discriminatorColumns = array();
  99. /**
  100. * Maps alias names to field names that should be used for indexing.
  101. *
  102. * @ignore
  103. * @var array
  104. */
  105. public $indexByMap = array();
  106. /**
  107. * Map from column names to class names that declare the field the column is mapped to.
  108. *
  109. * @ignore
  110. * @var array
  111. */
  112. public $declaringClasses = array();
  113. /**
  114. * This is necessary to hydrate derivate foreign keys correctly.
  115. *
  116. * @var array
  117. */
  118. public $isIdentifierColumn = array();
  119. /**
  120. * Adds an entity result to this ResultSetMapping.
  121. *
  122. * @param string $class The class name of the entity.
  123. * @param string $alias The alias for the class. The alias must be unique among all entity
  124. * results or joined entity results within this ResultSetMapping.
  125. * @todo Rename: addRootEntity
  126. */
  127. public function addEntityResult($class, $alias)
  128. {
  129. $this->aliasMap[$alias] = $class;
  130. }
  131. /**
  132. * Sets a discriminator column for an entity result or joined entity result.
  133. * The discriminator column will be used to determine the concrete class name to
  134. * instantiate.
  135. *
  136. * @param string $alias The alias of the entity result or joined entity result the discriminator
  137. * column should be used for.
  138. * @param string $discrColumn The name of the discriminator column in the SQL result set.
  139. * @todo Rename: addDiscriminatorColumn
  140. */
  141. public function setDiscriminatorColumn($alias, $discrColumn)
  142. {
  143. $this->discriminatorColumns[$alias] = $discrColumn;
  144. $this->columnOwnerMap[$discrColumn] = $alias;
  145. }
  146. /**
  147. * Sets a field to use for indexing an entity result or joined entity result.
  148. *
  149. * @param string $alias The alias of an entity result or joined entity result.
  150. * @param string $fieldName The name of the field to use for indexing.
  151. */
  152. public function addIndexBy($alias, $fieldName)
  153. {
  154. $this->indexByMap[$alias] = $fieldName;
  155. }
  156. /**
  157. * Checks whether an entity result or joined entity result with a given alias has
  158. * a field set for indexing.
  159. *
  160. * @param string $alias
  161. * @return boolean
  162. * @todo Rename: isIndexed($alias)
  163. */
  164. public function hasIndexBy($alias)
  165. {
  166. return isset($this->indexByMap[$alias]);
  167. }
  168. /**
  169. * Checks whether the column with the given name is mapped as a field result
  170. * as part of an entity result or joined entity result.
  171. *
  172. * @param string $columnName The name of the column in the SQL result set.
  173. * @return boolean
  174. * @todo Rename: isField
  175. */
  176. public function isFieldResult($columnName)
  177. {
  178. return isset($this->fieldMappings[$columnName]);
  179. }
  180. /**
  181. * Adds a field to the result that belongs to an entity or joined entity.
  182. *
  183. * @param string $alias The alias of the root entity or joined entity to which the field belongs.
  184. * @param string $columnName The name of the column in the SQL result set.
  185. * @param string $fieldName The name of the field on the declaring class.
  186. * @param string $declaringClass The name of the class that declares/owns the specified field.
  187. * When $alias refers to a superclass in a mapped hierarchy but
  188. * the field $fieldName is defined on a subclass, specify that here.
  189. * If not specified, the field is assumed to belong to the class
  190. * designated by $alias.
  191. * @todo Rename: addField
  192. */
  193. public function addFieldResult($alias, $columnName, $fieldName, $declaringClass = null)
  194. {
  195. // column name (in result set) => field name
  196. $this->fieldMappings[$columnName] = $fieldName;
  197. // column name => alias of owner
  198. $this->columnOwnerMap[$columnName] = $alias;
  199. // field name => class name of declaring class
  200. $this->declaringClasses[$columnName] = $declaringClass ?: $this->aliasMap[$alias];
  201. if ( ! $this->isMixed && $this->scalarMappings) {
  202. $this->isMixed = true;
  203. }
  204. }
  205. /**
  206. * Adds a joined entity result.
  207. *
  208. * @param string $class The class name of the joined entity.
  209. * @param string $alias The unique alias to use for the joined entity.
  210. * @param string $parentAlias The alias of the entity result that is the parent of this joined result.
  211. * @param object $relation The association field that connects the parent entity result with the joined entity result.
  212. * @todo Rename: addJoinedEntity
  213. */
  214. public function addJoinedEntityResult($class, $alias, $parentAlias, $relation)
  215. {
  216. $this->aliasMap[$alias] = $class;
  217. $this->parentAliasMap[$alias] = $parentAlias;
  218. $this->relationMap[$alias] = $relation;
  219. }
  220. /**
  221. * Adds a scalar result mapping.
  222. *
  223. * @param string $columnName The name of the column in the SQL result set.
  224. * @param string $alias The result alias with which the scalar result should be placed in the result structure.
  225. * @todo Rename: addScalar
  226. */
  227. public function addScalarResult($columnName, $alias)
  228. {
  229. $this->scalarMappings[$columnName] = $alias;
  230. if ( ! $this->isMixed && $this->fieldMappings) {
  231. $this->isMixed = true;
  232. }
  233. }
  234. /**
  235. * Checks whether a column with a given name is mapped as a scalar result.
  236. *
  237. * @param string $columName The name of the column in the SQL result set.
  238. * @return boolean
  239. * @todo Rename: isScalar
  240. */
  241. public function isScalarResult($columnName)
  242. {
  243. return isset($this->scalarMappings[$columnName]);
  244. }
  245. /**
  246. * Gets the name of the class of an entity result or joined entity result,
  247. * identified by the given unique alias.
  248. *
  249. * @param string $alias
  250. * @return string
  251. */
  252. public function getClassName($alias)
  253. {
  254. return $this->aliasMap[$alias];
  255. }
  256. /**
  257. * Gets the field alias for a column that is mapped as a scalar value.
  258. *
  259. * @param string $columnName The name of the column in the SQL result set.
  260. * @return string
  261. */
  262. public function getScalarAlias($columnName)
  263. {
  264. return $this->scalarMappings[$columnName];
  265. }
  266. /**
  267. * Gets the name of the class that owns a field mapping for the specified column.
  268. *
  269. * @param string $columnName
  270. * @return string
  271. */
  272. public function getDeclaringClass($columnName)
  273. {
  274. return $this->declaringClasses[$columnName];
  275. }
  276. /**
  277. *
  278. * @param string $alias
  279. * @return AssociationMapping
  280. */
  281. public function getRelation($alias)
  282. {
  283. return $this->relationMap[$alias];
  284. }
  285. /**
  286. *
  287. * @param string $alias
  288. * @return boolean
  289. */
  290. public function isRelation($alias)
  291. {
  292. return isset($this->relationMap[$alias]);
  293. }
  294. /**
  295. * Gets the alias of the class that owns a field mapping for the specified column.
  296. *
  297. * @param string $columnName
  298. * @return string
  299. */
  300. public function getEntityAlias($columnName)
  301. {
  302. return $this->columnOwnerMap[$columnName];
  303. }
  304. /**
  305. * Gets the parent alias of the given alias.
  306. *
  307. * @param string $alias
  308. * @return string
  309. */
  310. public function getParentAlias($alias)
  311. {
  312. return $this->parentAliasMap[$alias];
  313. }
  314. /**
  315. * Checks whether the given alias has a parent alias.
  316. *
  317. * @param string $alias
  318. * @return boolean
  319. */
  320. public function hasParentAlias($alias)
  321. {
  322. return isset($this->parentAliasMap[$alias]);
  323. }
  324. /**
  325. * Gets the field name for a column name.
  326. *
  327. * @param string $columnName
  328. * @return string
  329. */
  330. public function getFieldName($columnName)
  331. {
  332. return $this->fieldMappings[$columnName];
  333. }
  334. /**
  335. *
  336. * @return array
  337. */
  338. public function getAliasMap()
  339. {
  340. return $this->aliasMap;
  341. }
  342. /**
  343. * Gets the number of different entities that appear in the mapped result.
  344. *
  345. * @return integer
  346. */
  347. public function getEntityResultCount()
  348. {
  349. return count($this->aliasMap);
  350. }
  351. /**
  352. * Checks whether this ResultSetMapping defines a mixed result.
  353. * Mixed results can only occur in object and array (graph) hydration. In such a
  354. * case a mixed result means that scalar values are mixed with objects/array in
  355. * the result.
  356. *
  357. * @return boolean
  358. */
  359. public function isMixedResult()
  360. {
  361. return $this->isMixed;
  362. }
  363. /**
  364. * Adds a meta column (foreign key or discriminator column) to the result set.
  365. *
  366. * @param string $alias
  367. * @param string $columnName
  368. * @param string $fieldName
  369. * @param bool
  370. */
  371. public function addMetaResult($alias, $columnName, $fieldName, $isIdentifierColumn = false)
  372. {
  373. $this->metaMappings[$columnName] = $fieldName;
  374. $this->columnOwnerMap[$columnName] = $alias;
  375. if ($isIdentifierColumn) {
  376. $this->isIdentifierColumn[$alias][$columnName] = true;
  377. }
  378. }
  379. }