ResultSetMapping.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. * @ignore
  38. * @var boolean Whether the result is mixed (contains scalar values together with field values).
  39. */
  40. public $isMixed = false;
  41. /**
  42. * @ignore
  43. * @var array Maps alias names to class names.
  44. */
  45. public $aliasMap = array();
  46. /**
  47. * @ignore
  48. * @var array Maps alias names to related association field names.
  49. */
  50. public $relationMap = array();
  51. /**
  52. * @ignore
  53. * @var array Maps alias names to parent alias names.
  54. */
  55. public $parentAliasMap = array();
  56. /**
  57. * @ignore
  58. * @var array Maps column names in the result set to field names for each class.
  59. */
  60. public $fieldMappings = array();
  61. /**
  62. * @ignore
  63. * @var array Maps column names in the result set to the alias/field name to use in the mapped result.
  64. */
  65. public $scalarMappings = array();
  66. /**
  67. * @ignore
  68. * @var array Maps column names in the result set to the alias/field type to use in the mapped result.
  69. */
  70. public $typeMappings = array();
  71. /**
  72. * @ignore
  73. * @var array Maps entities in the result set to the alias name to use in the mapped result.
  74. */
  75. public $entityMappings = array();
  76. /**
  77. * @ignore
  78. * @var array Maps column names of meta columns (foreign keys, discriminator columns, ...) to field names.
  79. */
  80. public $metaMappings = array();
  81. /**
  82. * @ignore
  83. * @var array Maps column names in the result set to the alias they belong to.
  84. */
  85. public $columnOwnerMap = array();
  86. /**
  87. * @ignore
  88. * @var array List of columns in the result set that are used as discriminator columns.
  89. */
  90. public $discriminatorColumns = array();
  91. /**
  92. * @ignore
  93. * @var array Maps alias names to field names that should be used for indexing.
  94. */
  95. public $indexByMap = array();
  96. /**
  97. * @ignore
  98. * @var array Map from column names to class names that declare the field the column is mapped to.
  99. */
  100. public $declaringClasses = array();
  101. /**
  102. * @var array This is necessary to hydrate derivate foreign keys correctly.
  103. */
  104. public $isIdentifierColumn = array();
  105. /**
  106. * Adds an entity result to this ResultSetMapping.
  107. *
  108. * @param string $class The class name of the entity.
  109. * @param string $alias The alias for the class. The alias must be unique among all entity
  110. * results or joined entity results within this ResultSetMapping.
  111. * @param string $resultAlias The result alias with which the entity result should be
  112. * placed in the result structure.
  113. * @return ResultSetMapping This ResultSetMapping instance.
  114. * @todo Rename: addRootEntity
  115. */
  116. public function addEntityResult($class, $alias, $resultAlias = null)
  117. {
  118. $this->aliasMap[$alias] = $class;
  119. $this->entityMappings[$alias] = $resultAlias;
  120. if ($resultAlias !== null) {
  121. $this->isMixed = true;
  122. }
  123. return $this;
  124. }
  125. /**
  126. * Sets a discriminator column for an entity result or joined entity result.
  127. * The discriminator column will be used to determine the concrete class name to
  128. * instantiate.
  129. *
  130. * @param string $alias The alias of the entity result or joined entity result the discriminator
  131. * column should be used for.
  132. * @param string $discrColumn The name of the discriminator column in the SQL result set.
  133. * @return ResultSetMapping This ResultSetMapping instance.
  134. * @todo Rename: addDiscriminatorColumn
  135. */
  136. public function setDiscriminatorColumn($alias, $discrColumn)
  137. {
  138. $this->discriminatorColumns[$alias] = $discrColumn;
  139. $this->columnOwnerMap[$discrColumn] = $alias;
  140. return $this;
  141. }
  142. /**
  143. * Sets a field to use for indexing an entity result or joined entity result.
  144. *
  145. * @param string $alias The alias of an entity result or joined entity result.
  146. * @param string $fieldName The name of the field to use for indexing.
  147. * @return ResultSetMapping This ResultSetMapping instance.
  148. */
  149. public function addIndexBy($alias, $fieldName)
  150. {
  151. $found = false;
  152. foreach ($this->fieldMappings AS $columnName => $columnFieldName) {
  153. if ( ! ($columnFieldName === $fieldName && $this->columnOwnerMap[$columnName] === $alias)) continue;
  154. $this->addIndexByColumn($alias, $columnName);
  155. $found = true;
  156. break;
  157. }
  158. /* TODO: check if this exception can be put back, for now it's gone because of assumptions made by some ORM internals
  159. if ( ! $found) {
  160. $message = sprintf(
  161. 'Cannot add index by for DQL alias %s and field %s without calling addFieldResult() for them before.',
  162. $alias,
  163. $fieldName
  164. );
  165. throw new \LogicException($message);
  166. }
  167. */
  168. return $this;
  169. }
  170. /**
  171. * Set to index by a scalar result column name
  172. *
  173. * @param $resultColumnName
  174. * @return ResultSetMapping This ResultSetMapping instance.
  175. */
  176. public function addIndexByScalar($resultColumnName)
  177. {
  178. $this->indexByMap['scalars'] = $resultColumnName;
  179. return $this;
  180. }
  181. /**
  182. * Sets a column to use for indexing an entity or joined entity result by the given alias name.
  183. *
  184. * @param $alias
  185. * @param $resultColumnName
  186. * @return ResultSetMapping This ResultSetMapping instance.
  187. */
  188. public function addIndexByColumn($alias, $resultColumnName)
  189. {
  190. $this->indexByMap[$alias] = $resultColumnName;
  191. return $this;
  192. }
  193. /**
  194. * Checks whether an entity result or joined entity result with a given alias has
  195. * a field set for indexing.
  196. *
  197. * @param string $alias
  198. * @return boolean
  199. * @todo Rename: isIndexed($alias)
  200. */
  201. public function hasIndexBy($alias)
  202. {
  203. return isset($this->indexByMap[$alias]);
  204. }
  205. /**
  206. * Checks whether the column with the given name is mapped as a field result
  207. * as part of an entity result or joined entity result.
  208. *
  209. * @param string $columnName The name of the column in the SQL result set.
  210. * @return boolean
  211. * @todo Rename: isField
  212. */
  213. public function isFieldResult($columnName)
  214. {
  215. return isset($this->fieldMappings[$columnName]);
  216. }
  217. /**
  218. * Adds a field to the result that belongs to an entity or joined entity.
  219. *
  220. * @param string $alias The alias of the root entity or joined entity to which the field belongs.
  221. * @param string $columnName The name of the column in the SQL result set.
  222. * @param string $fieldName The name of the field on the declaring class.
  223. * @param string $declaringClass The name of the class that declares/owns the specified field.
  224. * When $alias refers to a superclass in a mapped hierarchy but
  225. * the field $fieldName is defined on a subclass, specify that here.
  226. * If not specified, the field is assumed to belong to the class
  227. * designated by $alias.
  228. * @return ResultSetMapping This ResultSetMapping instance.
  229. * @todo Rename: addField
  230. */
  231. public function addFieldResult($alias, $columnName, $fieldName, $declaringClass = null)
  232. {
  233. // column name (in result set) => field name
  234. $this->fieldMappings[$columnName] = $fieldName;
  235. // column name => alias of owner
  236. $this->columnOwnerMap[$columnName] = $alias;
  237. // field name => class name of declaring class
  238. $this->declaringClasses[$columnName] = $declaringClass ?: $this->aliasMap[$alias];
  239. if ( ! $this->isMixed && $this->scalarMappings) {
  240. $this->isMixed = true;
  241. }
  242. return $this;
  243. }
  244. /**
  245. * Adds a joined entity result.
  246. *
  247. * @param string $class The class name of the joined entity.
  248. * @param string $alias The unique alias to use for the joined entity.
  249. * @param string $parentAlias The alias of the entity result that is the parent of this joined result.
  250. * @param object $relation The association field that connects the parent entity result with the joined entity result.
  251. * @return ResultSetMapping This ResultSetMapping instance.
  252. * @todo Rename: addJoinedEntity
  253. */
  254. public function addJoinedEntityResult($class, $alias, $parentAlias, $relation)
  255. {
  256. $this->aliasMap[$alias] = $class;
  257. $this->parentAliasMap[$alias] = $parentAlias;
  258. $this->relationMap[$alias] = $relation;
  259. return $this;
  260. }
  261. /**
  262. * Adds a scalar result mapping.
  263. *
  264. * @param string $columnName The name of the column in the SQL result set.
  265. * @param string $alias The result alias with which the scalar result should be placed in the result structure.
  266. * @param string $type The column type
  267. *
  268. * @return ResultSetMapping This ResultSetMapping instance.
  269. *
  270. * @todo Rename: addScalar
  271. */
  272. public function addScalarResult($columnName, $alias, $type = null)
  273. {
  274. $this->scalarMappings[$columnName] = $alias;
  275. $this->typeMappings[$columnName] = $type ?: 'string';
  276. if ( ! $this->isMixed && $this->fieldMappings) {
  277. $this->isMixed = true;
  278. }
  279. return $this;
  280. }
  281. /**
  282. * Checks whether a column with a given name is mapped as a scalar result.
  283. *
  284. * @param string $columName The name of the column in the SQL result set.
  285. * @return boolean
  286. * @todo Rename: isScalar
  287. */
  288. public function isScalarResult($columnName)
  289. {
  290. return isset($this->scalarMappings[$columnName]);
  291. }
  292. /**
  293. * Gets the name of the class of an entity result or joined entity result,
  294. * identified by the given unique alias.
  295. *
  296. * @param string $alias
  297. * @return string
  298. */
  299. public function getClassName($alias)
  300. {
  301. return $this->aliasMap[$alias];
  302. }
  303. /**
  304. * Gets the field alias for a column that is mapped as a scalar value.
  305. *
  306. * @param string $columnName The name of the column in the SQL result set.
  307. * @return string
  308. */
  309. public function getScalarAlias($columnName)
  310. {
  311. return $this->scalarMappings[$columnName];
  312. }
  313. /**
  314. * Gets the name of the class that owns a field mapping for the specified column.
  315. *
  316. * @param string $columnName
  317. * @return string
  318. */
  319. public function getDeclaringClass($columnName)
  320. {
  321. return $this->declaringClasses[$columnName];
  322. }
  323. /**
  324. *
  325. * @param string $alias
  326. * @return AssociationMapping
  327. */
  328. public function getRelation($alias)
  329. {
  330. return $this->relationMap[$alias];
  331. }
  332. /**
  333. *
  334. * @param string $alias
  335. * @return boolean
  336. */
  337. public function isRelation($alias)
  338. {
  339. return isset($this->relationMap[$alias]);
  340. }
  341. /**
  342. * Gets the alias of the class that owns a field mapping for the specified column.
  343. *
  344. * @param string $columnName
  345. * @return string
  346. */
  347. public function getEntityAlias($columnName)
  348. {
  349. return $this->columnOwnerMap[$columnName];
  350. }
  351. /**
  352. * Gets the parent alias of the given alias.
  353. *
  354. * @param string $alias
  355. * @return string
  356. */
  357. public function getParentAlias($alias)
  358. {
  359. return $this->parentAliasMap[$alias];
  360. }
  361. /**
  362. * Checks whether the given alias has a parent alias.
  363. *
  364. * @param string $alias
  365. * @return boolean
  366. */
  367. public function hasParentAlias($alias)
  368. {
  369. return isset($this->parentAliasMap[$alias]);
  370. }
  371. /**
  372. * Gets the field name for a column name.
  373. *
  374. * @param string $columnName
  375. * @return string
  376. */
  377. public function getFieldName($columnName)
  378. {
  379. return $this->fieldMappings[$columnName];
  380. }
  381. /**
  382. *
  383. * @return array
  384. */
  385. public function getAliasMap()
  386. {
  387. return $this->aliasMap;
  388. }
  389. /**
  390. * Gets the number of different entities that appear in the mapped result.
  391. *
  392. * @return integer
  393. */
  394. public function getEntityResultCount()
  395. {
  396. return count($this->aliasMap);
  397. }
  398. /**
  399. * Checks whether this ResultSetMapping defines a mixed result.
  400. * Mixed results can only occur in object and array (graph) hydration. In such a
  401. * case a mixed result means that scalar values are mixed with objects/array in
  402. * the result.
  403. *
  404. * @return boolean
  405. */
  406. public function isMixedResult()
  407. {
  408. return $this->isMixed;
  409. }
  410. /**
  411. * Adds a meta column (foreign key or discriminator column) to the result set.
  412. *
  413. * @param string $alias
  414. * @param string $columnName
  415. * @param string $fieldName
  416. * @param bool
  417. * @return ResultSetMapping This ResultSetMapping instance.
  418. */
  419. public function addMetaResult($alias, $columnName, $fieldName, $isIdentifierColumn = false)
  420. {
  421. $this->metaMappings[$columnName] = $fieldName;
  422. $this->columnOwnerMap[$columnName] = $alias;
  423. if ($isIdentifierColumn) {
  424. $this->isIdentifierColumn[$alias][$columnName] = true;
  425. }
  426. return $this;
  427. }
  428. }