PageRenderTime 24ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/RestAPI/vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/SimpleObjectHydrator.php

https://gitlab.com/martinstti/silex-microframework-rest
PHP | 152 lines | 84 code | 31 blank | 37 comment | 16 complexity | 8d9f220889dde747aa0a6088b53f9e5d MD5 | raw file
  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\Internal\Hydration;
  20. use PDO;
  21. use Doctrine\DBAL\Types\Type;
  22. use Doctrine\ORM\Mapping\ClassMetadata;
  23. use Doctrine\ORM\Query;
  24. class SimpleObjectHydrator extends AbstractHydrator
  25. {
  26. /**
  27. * @var ClassMetadata
  28. */
  29. private $class;
  30. /**
  31. * {@inheritdoc}
  32. */
  33. protected function prepare()
  34. {
  35. if (count($this->_rsm->aliasMap) !== 1) {
  36. throw new \RuntimeException("Cannot use SimpleObjectHydrator with a ResultSetMapping that contains more than one object result.");
  37. }
  38. if ($this->_rsm->scalarMappings) {
  39. throw new \RuntimeException("Cannot use SimpleObjectHydrator with a ResultSetMapping that contains scalar mappings.");
  40. }
  41. $this->class = $this->getClassMetadata(reset($this->_rsm->aliasMap));
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. protected function cleanup()
  47. {
  48. parent::cleanup();
  49. $this->_uow->triggerEagerLoads();
  50. $this->_uow->hydrationComplete();
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. protected function hydrateAllData()
  56. {
  57. $result = array();
  58. while ($row = $this->_stmt->fetch(PDO::FETCH_ASSOC)) {
  59. $this->hydrateRowData($row, $result);
  60. }
  61. $this->_em->getUnitOfWork()->triggerEagerLoads();
  62. return $result;
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. protected function hydrateRowData(array $sqlResult, array &$result)
  68. {
  69. $entityName = $this->class->name;
  70. $data = array();
  71. // We need to find the correct entity class name if we have inheritance in resultset
  72. if ($this->class->inheritanceType !== ClassMetadata::INHERITANCE_TYPE_NONE) {
  73. $discrColumnName = $this->_platform->getSQLResultCasing($this->class->discriminatorColumn['name']);
  74. // Find mapped discriminator column from the result set.
  75. if ($metaMappingDiscrColumnName = array_search($discrColumnName, $this->_rsm->metaMappings)) {
  76. $discrColumnName = $metaMappingDiscrColumnName;
  77. }
  78. if ( ! isset($sqlResult[$discrColumnName])) {
  79. throw HydrationException::missingDiscriminatorColumn($entityName, $discrColumnName, key($this->_rsm->aliasMap));
  80. }
  81. if ($sqlResult[$discrColumnName] === '') {
  82. throw HydrationException::emptyDiscriminatorValue(key($this->_rsm->aliasMap));
  83. }
  84. $discrMap = $this->class->discriminatorMap;
  85. if ( ! isset($discrMap[$sqlResult[$discrColumnName]])) {
  86. throw HydrationException::invalidDiscriminatorValue($sqlResult[$discrColumnName], array_keys($discrMap));
  87. }
  88. $entityName = $discrMap[$sqlResult[$discrColumnName]];
  89. unset($sqlResult[$discrColumnName]);
  90. }
  91. foreach ($sqlResult as $column => $value) {
  92. // An ObjectHydrator should be used instead of SimpleObjectHydrator
  93. if (isset($this->_rsm->relationMap[$column])) {
  94. throw new \Exception(sprintf('Unable to retrieve association information for column "%s"', $column));
  95. }
  96. $cacheKeyInfo = $this->hydrateColumnInfo($column);
  97. if ( ! $cacheKeyInfo) {
  98. continue;
  99. }
  100. // Convert field to a valid PHP value
  101. if (isset($cacheKeyInfo['type'])) {
  102. $type = $cacheKeyInfo['type'];
  103. $value = $type->convertToPHPValue($value, $this->_platform);
  104. }
  105. $fieldName = $cacheKeyInfo['fieldName'];
  106. // Prevent overwrite in case of inherit classes using same property name (See AbstractHydrator)
  107. if ( ! isset($data[$fieldName]) || $value !== null) {
  108. $data[$fieldName] = $value;
  109. }
  110. }
  111. if (isset($this->_hints[Query::HINT_REFRESH_ENTITY])) {
  112. $this->registerManaged($this->class, $this->_hints[Query::HINT_REFRESH_ENTITY], $data);
  113. }
  114. $uow = $this->_em->getUnitOfWork();
  115. $entity = $uow->createEntity($entityName, $data, $this->_hints);
  116. $result[] = $entity;
  117. if (isset($this->_hints[Query::HINT_INTERNAL_ITERATION]) && $this->_hints[Query::HINT_INTERNAL_ITERATION]) {
  118. $this->_uow->hydrationComplete();
  119. }
  120. }
  121. }