ResultSetMapping.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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 MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\ORM\Query;
  20. use function array_merge;
  21. use function count;
  22. /**
  23. * A ResultSetMapping describes how a result set of an SQL query maps to a Doctrine result.
  24. *
  25. * IMPORTANT NOTE:
  26. * The properties of this class are only public for fast internal READ access and to (drastically)
  27. * reduce the size of serialized instances for more effective caching due to better (un-)serialization
  28. * performance.
  29. *
  30. * <b>Users should use the public methods.</b>
  31. *
  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 bool
  41. */
  42. public $isMixed = false;
  43. /**
  44. * Whether the result is a select statement.
  45. *
  46. * @ignore
  47. * @var bool
  48. */
  49. public $isSelect = true;
  50. /**
  51. * Maps alias names to class names.
  52. *
  53. * @ignore
  54. * @psalm-var array<string, class-string>
  55. */
  56. public $aliasMap = [];
  57. /**
  58. * Maps alias names to related association field names.
  59. *
  60. * @ignore
  61. * @psalm-var array<string, string>
  62. */
  63. public $relationMap = [];
  64. /**
  65. * Maps alias names to parent alias names.
  66. *
  67. * @ignore
  68. * @psalm-var array<string, string>
  69. */
  70. public $parentAliasMap = [];
  71. /**
  72. * Maps column names in the result set to field names for each class.
  73. *
  74. * @ignore
  75. * @psalm-var array<string, string>
  76. */
  77. public $fieldMappings = [];
  78. /**
  79. * Maps column names in the result set to the alias/field name to use in the mapped result.
  80. *
  81. * @ignore
  82. * @psalm-var array<string, string>
  83. */
  84. public $scalarMappings = [];
  85. /**
  86. * Maps column names in the result set to the alias/field type to use in the mapped result.
  87. *
  88. * @ignore
  89. * @psalm-var array<string, string>
  90. */
  91. public $typeMappings = [];
  92. /**
  93. * Maps entities in the result set to the alias name to use in the mapped result.
  94. *
  95. * @ignore
  96. * @psalm-var array<string, string|null>
  97. */
  98. public $entityMappings = [];
  99. /**
  100. * Maps column names of meta columns (foreign keys, discriminator columns, ...) to field names.
  101. *
  102. * @ignore
  103. * @psalm-var array<string, string>
  104. */
  105. public $metaMappings = [];
  106. /**
  107. * Maps column names in the result set to the alias they belong to.
  108. *
  109. * @ignore
  110. * @psalm-var array<string, string>
  111. */
  112. public $columnOwnerMap = [];
  113. /**
  114. * List of columns in the result set that are used as discriminator columns.
  115. *
  116. * @ignore
  117. * @psalm-var array<string, string>
  118. */
  119. public $discriminatorColumns = [];
  120. /**
  121. * Maps alias names to field names that should be used for indexing.
  122. *
  123. * @ignore
  124. * @psalm-var array<string, string>
  125. */
  126. public $indexByMap = [];
  127. /**
  128. * Map from column names to class names that declare the field the column is mapped to.
  129. *
  130. * @ignore
  131. * @psalm-var array<string, class-string>
  132. */
  133. public $declaringClasses = [];
  134. /**
  135. * This is necessary to hydrate derivate foreign keys correctly.
  136. *
  137. * @psalm-var array<string, array<string, bool>>
  138. */
  139. public $isIdentifierColumn = [];
  140. /**
  141. * Maps column names in the result set to field names for each new object expression.
  142. *
  143. * @psalm-var array<string, array<string, mixed>>
  144. */
  145. public $newObjectMappings = [];
  146. /**
  147. * Maps metadata parameter names to the metadata attribute.
  148. *
  149. * @psalm-var array<int|string, string>
  150. */
  151. public $metadataParameterMapping = [];
  152. /**
  153. * Contains query parameter names to be resolved as discriminator values
  154. *
  155. * @psalm-var array<string, string>
  156. */
  157. public $discriminatorParameters = [];
  158. /**
  159. * Adds an entity result to this ResultSetMapping.
  160. *
  161. * @param string $class The class name of the entity.
  162. * @param string $alias The alias for the class. The alias must be unique among all entity
  163. * results or joined entity results within this ResultSetMapping.
  164. * @param string|null $resultAlias The result alias with which the entity result should be
  165. * placed in the result structure.
  166. *
  167. * @return static This ResultSetMapping instance.
  168. *
  169. * @todo Rename: addRootEntity
  170. */
  171. public function addEntityResult($class, $alias, $resultAlias = null)
  172. {
  173. $this->aliasMap[$alias] = $class;
  174. $this->entityMappings[$alias] = $resultAlias;
  175. if ($resultAlias !== null) {
  176. $this->isMixed = true;
  177. }
  178. return $this;
  179. }
  180. /**
  181. * Sets a discriminator column for an entity result or joined entity result.
  182. * The discriminator column will be used to determine the concrete class name to
  183. * instantiate.
  184. *
  185. * @param string $alias The alias of the entity result or joined entity result the discriminator
  186. * column should be used for.
  187. * @param string $discrColumn The name of the discriminator column in the SQL result set.
  188. *
  189. * @return static This ResultSetMapping instance.
  190. *
  191. * @todo Rename: addDiscriminatorColumn
  192. */
  193. public function setDiscriminatorColumn($alias, $discrColumn)
  194. {
  195. $this->discriminatorColumns[$alias] = $discrColumn;
  196. $this->columnOwnerMap[$discrColumn] = $alias;
  197. return $this;
  198. }
  199. /**
  200. * Sets a field to use for indexing an entity result or joined entity result.
  201. *
  202. * @param string $alias The alias of an entity result or joined entity result.
  203. * @param string $fieldName The name of the field to use for indexing.
  204. *
  205. * @return static This ResultSetMapping instance.
  206. */
  207. public function addIndexBy($alias, $fieldName)
  208. {
  209. $found = false;
  210. foreach (array_merge($this->metaMappings, $this->fieldMappings) as $columnName => $columnFieldName) {
  211. if (! ($columnFieldName === $fieldName && $this->columnOwnerMap[$columnName] === $alias)) {
  212. continue;
  213. }
  214. $this->addIndexByColumn($alias, $columnName);
  215. $found = true;
  216. break;
  217. }
  218. /* TODO: check if this exception can be put back, for now it's gone because of assumptions made by some ORM internals
  219. if ( ! $found) {
  220. $message = sprintf(
  221. 'Cannot add index by for DQL alias %s and field %s without calling addFieldResult() for them before.',
  222. $alias,
  223. $fieldName
  224. );
  225. throw new \LogicException($message);
  226. }
  227. */
  228. return $this;
  229. }
  230. /**
  231. * Sets to index by a scalar result column name.
  232. *
  233. * @param string $resultColumnName
  234. *
  235. * @return static This ResultSetMapping instance.
  236. */
  237. public function addIndexByScalar($resultColumnName)
  238. {
  239. $this->indexByMap['scalars'] = $resultColumnName;
  240. return $this;
  241. }
  242. /**
  243. * Sets a column to use for indexing an entity or joined entity result by the given alias name.
  244. *
  245. * @param string $alias
  246. * @param string $resultColumnName
  247. *
  248. * @return static This ResultSetMapping instance.
  249. */
  250. public function addIndexByColumn($alias, $resultColumnName)
  251. {
  252. $this->indexByMap[$alias] = $resultColumnName;
  253. return $this;
  254. }
  255. /**
  256. * Checks whether an entity result or joined entity result with a given alias has
  257. * a field set for indexing.
  258. *
  259. * @param string $alias
  260. *
  261. * @return bool
  262. *
  263. * @todo Rename: isIndexed($alias)
  264. */
  265. public function hasIndexBy($alias)
  266. {
  267. return isset($this->indexByMap[$alias]);
  268. }
  269. /**
  270. * Checks whether the column with the given name is mapped as a field result
  271. * as part of an entity result or joined entity result.
  272. *
  273. * @param string $columnName The name of the column in the SQL result set.
  274. *
  275. * @return bool
  276. *
  277. * @todo Rename: isField
  278. */
  279. public function isFieldResult($columnName)
  280. {
  281. return isset($this->fieldMappings[$columnName]);
  282. }
  283. /**
  284. * Adds a field to the result that belongs to an entity or joined entity.
  285. *
  286. * @param string $alias The alias of the root entity or joined entity to which the field belongs.
  287. * @param string $columnName The name of the column in the SQL result set.
  288. * @param string $fieldName The name of the field on the declaring class.
  289. * @param string|null $declaringClass The name of the class that declares/owns the specified field.
  290. * When $alias refers to a superclass in a mapped hierarchy but
  291. * the field $fieldName is defined on a subclass, specify that here.
  292. * If not specified, the field is assumed to belong to the class
  293. * designated by $alias.
  294. *
  295. * @return static This ResultSetMapping instance.
  296. *
  297. * @todo Rename: addField
  298. */
  299. public function addFieldResult($alias, $columnName, $fieldName, $declaringClass = null)
  300. {
  301. // column name (in result set) => field name
  302. $this->fieldMappings[$columnName] = $fieldName;
  303. // column name => alias of owner
  304. $this->columnOwnerMap[$columnName] = $alias;
  305. // field name => class name of declaring class
  306. $this->declaringClasses[$columnName] = $declaringClass ?: $this->aliasMap[$alias];
  307. if (! $this->isMixed && $this->scalarMappings) {
  308. $this->isMixed = true;
  309. }
  310. return $this;
  311. }
  312. /**
  313. * Adds a joined entity result.
  314. *
  315. * @param string $class The class name of the joined entity.
  316. * @param string $alias The unique alias to use for the joined entity.
  317. * @param string $parentAlias The alias of the entity result that is the parent of this joined result.
  318. * @param string $relation The association field that connects the parent entity result
  319. * with the joined entity result.
  320. *
  321. * @return static This ResultSetMapping instance.
  322. *
  323. * @todo Rename: addJoinedEntity
  324. */
  325. public function addJoinedEntityResult($class, $alias, $parentAlias, $relation)
  326. {
  327. $this->aliasMap[$alias] = $class;
  328. $this->parentAliasMap[$alias] = $parentAlias;
  329. $this->relationMap[$alias] = $relation;
  330. return $this;
  331. }
  332. /**
  333. * Adds a scalar result mapping.
  334. *
  335. * @param string $columnName The name of the column in the SQL result set.
  336. * @param string $alias The result alias with which the scalar result should be placed in the result structure.
  337. * @param string $type The column type
  338. *
  339. * @return static This ResultSetMapping instance.
  340. *
  341. * @todo Rename: addScalar
  342. */
  343. public function addScalarResult($columnName, $alias, $type = 'string')
  344. {
  345. $this->scalarMappings[$columnName] = $alias;
  346. $this->typeMappings[$columnName] = $type;
  347. if (! $this->isMixed && $this->fieldMappings) {
  348. $this->isMixed = true;
  349. }
  350. return $this;
  351. }
  352. /**
  353. * Adds a metadata parameter mappings.
  354. *
  355. * @param mixed $parameter The parameter name in the SQL result set.
  356. * @param string $attribute The metadata attribute.
  357. */
  358. public function addMetadataParameterMapping($parameter, $attribute)
  359. {
  360. $this->metadataParameterMapping[$parameter] = $attribute;
  361. }
  362. /**
  363. * Checks whether a column with a given name is mapped as a scalar result.
  364. *
  365. * @param string $columnName The name of the column in the SQL result set.
  366. *
  367. * @return bool
  368. *
  369. * @todo Rename: isScalar
  370. */
  371. public function isScalarResult($columnName)
  372. {
  373. return isset($this->scalarMappings[$columnName]);
  374. }
  375. /**
  376. * Gets the name of the class of an entity result or joined entity result,
  377. * identified by the given unique alias.
  378. *
  379. * @param string $alias
  380. *
  381. * @return string
  382. */
  383. public function getClassName($alias)
  384. {
  385. return $this->aliasMap[$alias];
  386. }
  387. /**
  388. * Gets the field alias for a column that is mapped as a scalar value.
  389. *
  390. * @param string $columnName The name of the column in the SQL result set.
  391. *
  392. * @return string
  393. */
  394. public function getScalarAlias($columnName)
  395. {
  396. return $this->scalarMappings[$columnName];
  397. }
  398. /**
  399. * Gets the name of the class that owns a field mapping for the specified column.
  400. *
  401. * @param string $columnName
  402. *
  403. * @return string
  404. */
  405. public function getDeclaringClass($columnName)
  406. {
  407. return $this->declaringClasses[$columnName];
  408. }
  409. /**
  410. * @param string $alias
  411. *
  412. * @return string
  413. */
  414. public function getRelation($alias)
  415. {
  416. return $this->relationMap[$alias];
  417. }
  418. /**
  419. * @param string $alias
  420. *
  421. * @return bool
  422. */
  423. public function isRelation($alias)
  424. {
  425. return isset($this->relationMap[$alias]);
  426. }
  427. /**
  428. * Gets the alias of the class that owns a field mapping for the specified column.
  429. *
  430. * @param string $columnName
  431. *
  432. * @return string
  433. */
  434. public function getEntityAlias($columnName)
  435. {
  436. return $this->columnOwnerMap[$columnName];
  437. }
  438. /**
  439. * Gets the parent alias of the given alias.
  440. *
  441. * @param string $alias
  442. *
  443. * @return string
  444. */
  445. public function getParentAlias($alias)
  446. {
  447. return $this->parentAliasMap[$alias];
  448. }
  449. /**
  450. * Checks whether the given alias has a parent alias.
  451. *
  452. * @param string $alias
  453. *
  454. * @return bool
  455. */
  456. public function hasParentAlias($alias)
  457. {
  458. return isset($this->parentAliasMap[$alias]);
  459. }
  460. /**
  461. * Gets the field name for a column name.
  462. *
  463. * @param string $columnName
  464. *
  465. * @return string
  466. */
  467. public function getFieldName($columnName)
  468. {
  469. return $this->fieldMappings[$columnName];
  470. }
  471. /**
  472. * @psalm-return array<string, class-string>
  473. */
  474. public function getAliasMap()
  475. {
  476. return $this->aliasMap;
  477. }
  478. /**
  479. * Gets the number of different entities that appear in the mapped result.
  480. *
  481. * @return int
  482. */
  483. public function getEntityResultCount()
  484. {
  485. return count($this->aliasMap);
  486. }
  487. /**
  488. * Checks whether this ResultSetMapping defines a mixed result.
  489. *
  490. * Mixed results can only occur in object and array (graph) hydration. In such a
  491. * case a mixed result means that scalar values are mixed with objects/array in
  492. * the result.
  493. *
  494. * @return bool
  495. */
  496. public function isMixedResult()
  497. {
  498. return $this->isMixed;
  499. }
  500. /**
  501. * Adds a meta column (foreign key or discriminator column) to the result set.
  502. *
  503. * @param string $alias The result alias with which the meta result should be placed in the result structure.
  504. * @param string $columnName The name of the column in the SQL result set.
  505. * @param string $fieldName The name of the field on the declaring class.
  506. * @param bool $isIdentifierColumn
  507. * @param string $type The column type
  508. *
  509. * @return static This ResultSetMapping instance.
  510. *
  511. * @todo Make all methods of this class require all parameters and not infer anything
  512. */
  513. public function addMetaResult($alias, $columnName, $fieldName, $isIdentifierColumn = false, $type = null)
  514. {
  515. $this->metaMappings[$columnName] = $fieldName;
  516. $this->columnOwnerMap[$columnName] = $alias;
  517. if ($isIdentifierColumn) {
  518. $this->isIdentifierColumn[$alias][$columnName] = true;
  519. }
  520. if ($type) {
  521. $this->typeMappings[$columnName] = $type;
  522. }
  523. return $this;
  524. }
  525. }