ResultSetMappingBuilder.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 Doctrine\ORM\EntityManager;
  21. use Doctrine\ORM\Mapping\ClassMetadataInfo;
  22. /**
  23. * A ResultSetMappingBuilder uses the EntityManager to automatically populate entity fields
  24. *
  25. * @author Michael Ridgway <mcridgway@gmail.com>
  26. * @since 2.1
  27. */
  28. class ResultSetMappingBuilder extends ResultSetMapping
  29. {
  30. /**
  31. * @var EntityManager
  32. */
  33. private $em;
  34. /**
  35. * @param EntityManager
  36. */
  37. public function __construct(EntityManager $em)
  38. {
  39. $this->em = $em;
  40. }
  41. /**
  42. * Adds a root entity and all of its fields to the result set.
  43. *
  44. * @param string $class The class name of the root entity.
  45. * @param string $alias The unique alias to use for the root entity.
  46. * @param array $renamedColumns Columns that have been renamed (tableColumnName => queryColumnName)
  47. */
  48. public function addRootEntityFromClassMetadata($class, $alias, $renamedColumns = array())
  49. {
  50. $this->addEntityResult($class, $alias);
  51. $this->addAllClassFields($class, $alias, $renamedColumns);
  52. }
  53. /**
  54. * Adds a joined entity and all of its fields to the result set.
  55. *
  56. * @param string $class The class name of the joined entity.
  57. * @param string $alias The unique alias to use for the joined entity.
  58. * @param string $parentAlias The alias of the entity result that is the parent of this joined result.
  59. * @param object $relation The association field that connects the parent entity result with the joined entity result.
  60. * @param array $renamedColumns Columns that have been renamed (tableColumnName => queryColumnName)
  61. */
  62. public function addJoinedEntityFromClassMetadata($class, $alias, $parentAlias, $relation, $renamedColumns = array())
  63. {
  64. $this->addJoinedEntityResult($class, $alias, $parentAlias, $relation);
  65. $this->addAllClassFields($class, $alias, $renamedColumns);
  66. }
  67. /**
  68. * Adds all fields of the given class to the result set mapping (columns and meta fields)
  69. */
  70. protected function addAllClassFields($class, $alias, $renamedColumns = array())
  71. {
  72. $classMetadata = $this->em->getClassMetadata($class);
  73. if ($classMetadata->isInheritanceTypeSingleTable() || $classMetadata->isInheritanceTypeJoined()) {
  74. throw new \InvalidArgumentException('ResultSetMapping builder does not currently support inheritance.');
  75. }
  76. $platform = $this->em->getConnection()->getDatabasePlatform();
  77. foreach ($classMetadata->getColumnNames() as $columnName) {
  78. $propertyName = $classMetadata->getFieldName($columnName);
  79. if (isset($renamedColumns[$columnName])) {
  80. $columnName = $renamedColumns[$columnName];
  81. }
  82. $columnName = $platform->getSQLResultCasing($columnName);
  83. if (isset($this->fieldMappings[$columnName])) {
  84. throw new \InvalidArgumentException("The column '$columnName' conflicts with another column in the mapper.");
  85. }
  86. $this->addFieldResult($alias, $columnName, $propertyName);
  87. }
  88. foreach ($classMetadata->associationMappings as $associationMapping) {
  89. if ($associationMapping['isOwningSide'] && $associationMapping['type'] & ClassMetadataInfo::TO_ONE) {
  90. foreach ($associationMapping['joinColumns'] as $joinColumn) {
  91. $columnName = $joinColumn['name'];
  92. $renamedColumnName = isset($renamedColumns[$columnName]) ? $renamedColumns[$columnName] : $columnName;
  93. $renamedColumnName = $platform->getSQLResultCasing($renamedColumnName);
  94. if (isset($this->metaMappings[$renamedColumnName])) {
  95. throw new \InvalidArgumentException("The column '$renamedColumnName' conflicts with another column in the mapper.");
  96. }
  97. $this->addMetaResult($alias, $renamedColumnName, $columnName);
  98. }
  99. }
  100. }
  101. }
  102. /**
  103. * Adds the mappings of the results of native SQL queries to the result set.
  104. *
  105. * @param ClassMetadataInfo $class
  106. * @param array $queryMapping
  107. * @return ResultSetMappingBuilder
  108. */
  109. public function addNamedNativeQueryMapping(ClassMetadataInfo $class, array $queryMapping)
  110. {
  111. if (isset($queryMapping['resultClass'])) {
  112. return $this->addNamedNativeQueryResultClassMapping($class, $queryMapping['resultClass']);
  113. }
  114. return $this->addNamedNativeQueryResultSetMapping($class, $queryMapping['resultSetMapping']);
  115. }
  116. /**
  117. * Adds the class mapping of the results of native SQL queries to the result set.
  118. *
  119. * @param ClassMetadataInfo $class
  120. * @param string $resultClassName
  121. * @return ResultSetMappingBuilder
  122. */
  123. public function addNamedNativeQueryResultClassMapping(ClassMetadataInfo $class, $resultClassName)
  124. {
  125. $classMetadata = $this->em->getClassMetadata($resultClassName);
  126. $shortName = $classMetadata->reflClass->getShortName();
  127. $alias = strtolower($shortName[0]).'0';
  128. $this->addEntityResult($class->name, $alias);
  129. if ($classMetadata->discriminatorColumn) {
  130. $discriminatorColumn = $classMetadata->discriminatorColumn;
  131. $this->setDiscriminatorColumn($alias, $discriminatorColumn['name']);
  132. $this->addMetaResult($alias, $discriminatorColumn['name'], $discriminatorColumn['fieldName']);
  133. }
  134. foreach ($classMetadata->getColumnNames() as $key => $columnName) {
  135. $propertyName = $classMetadata->getFieldName($columnName);
  136. $this->addFieldResult($alias, $columnName, $propertyName);
  137. }
  138. foreach ($classMetadata->associationMappings as $associationMapping) {
  139. if ($associationMapping['isOwningSide'] && $associationMapping['type'] & ClassMetadataInfo::TO_ONE) {
  140. foreach ($associationMapping['joinColumns'] as $joinColumn) {
  141. $columnName = $joinColumn['name'];
  142. $this->addMetaResult($alias, $columnName, $columnName, $classMetadata->isIdentifier($columnName));
  143. }
  144. }
  145. }
  146. return $this;
  147. }
  148. /**
  149. * Adds the result set mapping of the results of native SQL queries to the result set.
  150. *
  151. * @param ClassMetadataInfo $class
  152. * @param string $resultSetMappingName
  153. * @return ResultSetMappingBuilder
  154. */
  155. public function addNamedNativeQueryResultSetMapping(ClassMetadataInfo $class, $resultSetMappingName)
  156. {
  157. $counter = 0;
  158. $resultMapping = $class->getSqlResultSetMapping($resultSetMappingName);
  159. $rooShortName = $class->reflClass->getShortName();
  160. $rootAlias = strtolower($rooShortName[0]) . $counter;
  161. if (isset($resultMapping['entities'])) {
  162. foreach ($resultMapping['entities'] as $key => $entityMapping) {
  163. $classMetadata = $this->em->getClassMetadata($entityMapping['entityClass']);
  164. if ($class->reflClass->name == $classMetadata->reflClass->name) {
  165. $this->addEntityResult($classMetadata->name, $rootAlias);
  166. $this->addNamedNativeQueryEntityResultMapping($classMetadata, $entityMapping, $rootAlias);
  167. } else {
  168. $shortName = $classMetadata->reflClass->getShortName();
  169. $joinAlias = strtolower($shortName[0]) . ++ $counter;
  170. $associations = $class->getAssociationsByTargetClass($classMetadata->name);
  171. foreach ($associations as $relation => $mapping) {
  172. $this->addJoinedEntityResult($mapping['targetEntity'], $joinAlias, $rootAlias, $relation);
  173. $this->addNamedNativeQueryEntityResultMapping($classMetadata, $entityMapping, $joinAlias);
  174. }
  175. }
  176. }
  177. }
  178. if (isset($resultMapping['columns'])) {
  179. foreach ($resultMapping['columns'] as $entityMapping) {
  180. $this->addScalarResult($entityMapping['name'], $entityMapping['name']);
  181. }
  182. }
  183. return $this;
  184. }
  185. /**
  186. * Adds the entity result mapping of the results of native SQL queries to the result set.
  187. *
  188. * @param ClassMetadataInfo $classMetadata
  189. * @param array $entityMapping
  190. * @param string $alias
  191. * @return ResultSetMappingBuilder
  192. */
  193. public function addNamedNativeQueryEntityResultMapping(ClassMetadataInfo $classMetadata, array $entityMapping, $alias)
  194. {
  195. if (isset($entityMapping['discriminatorColumn']) && $entityMapping['discriminatorColumn']) {
  196. $discriminatorColumn = $entityMapping['discriminatorColumn'];
  197. $this->setDiscriminatorColumn($alias, $discriminatorColumn);
  198. $this->addMetaResult($alias, $discriminatorColumn, $discriminatorColumn);
  199. }
  200. if (isset($entityMapping['fields']) && !empty($entityMapping['fields'])) {
  201. foreach ($entityMapping['fields'] as $field) {
  202. $fieldName = $field['name'];
  203. $relation = null;
  204. if(strpos($fieldName, '.')){
  205. list($relation, $fieldName) = explode('.', $fieldName);
  206. }
  207. if (isset($classMetadata->associationMappings[$relation])) {
  208. if($relation) {
  209. $associationMapping = $classMetadata->associationMappings[$relation];
  210. $joinAlias = $alias.$relation;
  211. $parentAlias = $alias;
  212. $this->addJoinedEntityResult($associationMapping['targetEntity'], $joinAlias, $parentAlias, $relation);
  213. $this->addFieldResult($joinAlias, $field['column'], $fieldName);
  214. }else {
  215. $this->addFieldResult($alias, $field['column'], $fieldName, $classMetadata->name);
  216. }
  217. } else {
  218. if(!isset($classMetadata->fieldMappings[$fieldName])) {
  219. throw new \InvalidArgumentException("Entity '".$classMetadata->name."' has no field '".$fieldName."'. ");
  220. }
  221. $this->addFieldResult($alias, $field['column'], $fieldName, $classMetadata->name);
  222. }
  223. }
  224. } else {
  225. foreach ($classMetadata->getColumnNames() as $columnName) {
  226. $propertyName = $classMetadata->getFieldName($columnName);
  227. $this->addFieldResult($alias, $columnName, $propertyName);
  228. }
  229. }
  230. return $this;
  231. }
  232. }