ResultSetMappingBuilder.php 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. 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. if (isset($this->fieldMappings[$columnName])) {
  83. throw new \InvalidArgumentException("The column '$columnName' conflicts with another column in the mapper.");
  84. }
  85. $this->addFieldResult($alias, $platform->getSQLResultCasing($columnName), $propertyName);
  86. }
  87. foreach ($classMetadata->associationMappings AS $associationMapping) {
  88. if ($associationMapping['isOwningSide'] && $associationMapping['type'] & ClassMetadataInfo::TO_ONE) {
  89. foreach ($associationMapping['joinColumns'] AS $joinColumn) {
  90. $columnName = $joinColumn['name'];
  91. $renamedColumnName = isset($renamedColumns[$columnName]) ? $renamedColumns[$columnName] : $columnName;
  92. if (isset($this->metaMappings[$renamedColumnName])) {
  93. throw new \InvalidArgumentException("The column '$renamedColumnName' conflicts with another column in the mapper.");
  94. }
  95. $this->addMetaResult($alias, $platform->getSQLResultCasing($renamedColumnName), $platform->getSQLResultCasing($columnName));
  96. }
  97. }
  98. }
  99. }
  100. }