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 | |
---|
20 | namespace Doctrine\ORM\Query; |
---|
21 | |
---|
22 | use Doctrine\ORM\EntityManager; |
---|
23 | use Doctrine\ORM\Mapping\ClassMetadataInfo; |
---|
24 | |
---|
25 | /** |
---|
26 | * A ResultSetMappingBuilder uses the EntityManager to automatically populate entity fields |
---|
27 | * |
---|
28 | * @author Michael Ridgway <mcridgway@gmail.com> |
---|
29 | * @since 2.1 |
---|
30 | */ |
---|
31 | class ResultSetMappingBuilder extends ResultSetMapping |
---|
32 | { |
---|
33 | /** |
---|
34 | * @var EntityManager |
---|
35 | */ |
---|
36 | private $em; |
---|
37 | |
---|
38 | /** |
---|
39 | * @param EntityManager |
---|
40 | */ |
---|
41 | public function __construct(EntityManager $em) |
---|
42 | { |
---|
43 | $this->em = $em; |
---|
44 | } |
---|
45 | |
---|
46 | /** |
---|
47 | * Adds a root entity and all of its fields to the result set. |
---|
48 | * |
---|
49 | * @param string $class The class name of the root entity. |
---|
50 | * @param string $alias The unique alias to use for the root entity. |
---|
51 | * @param array $renamedColumns Columns that have been renamed (tableColumnName => queryColumnName) |
---|
52 | */ |
---|
53 | public function addRootEntityFromClassMetadata($class, $alias, $renamedColumns = array()) |
---|
54 | { |
---|
55 | $this->addEntityResult($class, $alias); |
---|
56 | $this->addAllClassFields($class, $alias, $renamedColumns); |
---|
57 | } |
---|
58 | |
---|
59 | /** |
---|
60 | * Adds a joined entity and all of its fields to the result set. |
---|
61 | * |
---|
62 | * @param string $class The class name of the joined entity. |
---|
63 | * @param string $alias The unique alias to use for the joined entity. |
---|
64 | * @param string $parentAlias The alias of the entity result that is the parent of this joined result. |
---|
65 | * @param object $relation The association field that connects the parent entity result with the joined entity result. |
---|
66 | * @param array $renamedColumns Columns that have been renamed (tableColumnName => queryColumnName) |
---|
67 | */ |
---|
68 | public function addJoinedEntityFromClassMetadata($class, $alias, $parentAlias, $relation, $renamedColumns = array()) |
---|
69 | { |
---|
70 | $this->addJoinedEntityResult($class, $alias, $parentAlias, $relation); |
---|
71 | $this->addAllClassFields($class, $alias, $renamedColumns); |
---|
72 | } |
---|
73 | |
---|
74 | /** |
---|
75 | * Adds all fields of the given class to the result set mapping (columns and meta fields) |
---|
76 | */ |
---|
77 | protected function addAllClassFields($class, $alias, $renamedColumns = array()) |
---|
78 | { |
---|
79 | $classMetadata = $this->em->getClassMetadata($class); |
---|
80 | if ($classMetadata->isInheritanceTypeSingleTable() || $classMetadata->isInheritanceTypeJoined()) { |
---|
81 | throw new \InvalidArgumentException('ResultSetMapping builder does not currently support inheritance.'); |
---|
82 | } |
---|
83 | $platform = $this->em->getConnection()->getDatabasePlatform(); |
---|
84 | foreach ($classMetadata->getColumnNames() AS $columnName) { |
---|
85 | $propertyName = $classMetadata->getFieldName($columnName); |
---|
86 | if (isset($renamedColumns[$columnName])) { |
---|
87 | $columnName = $renamedColumns[$columnName]; |
---|
88 | } |
---|
89 | $columnName = $platform->getSQLResultCasing($columnName); |
---|
90 | if (isset($this->fieldMappings[$columnName])) { |
---|
91 | throw new \InvalidArgumentException("The column '$columnName' conflicts with another column in the mapper."); |
---|
92 | } |
---|
93 | $this->addFieldResult($alias, $columnName, $propertyName); |
---|
94 | } |
---|
95 | foreach ($classMetadata->associationMappings AS $associationMapping) { |
---|
96 | if ($associationMapping['isOwningSide'] && $associationMapping['type'] & ClassMetadataInfo::TO_ONE) { |
---|
97 | foreach ($associationMapping['joinColumns'] AS $joinColumn) { |
---|
98 | $columnName = $joinColumn['name']; |
---|
99 | $renamedColumnName = isset($renamedColumns[$columnName]) ? $renamedColumns[$columnName] : $columnName; |
---|
100 | $renamedColumnName = $platform->getSQLResultCasing($renamedColumnName); |
---|
101 | if (isset($this->metaMappings[$renamedColumnName])) { |
---|
102 | throw new \InvalidArgumentException("The column '$renamedColumnName' conflicts with another column in the mapper."); |
---|
103 | } |
---|
104 | $this->addMetaResult($alias, $renamedColumnName, $columnName); |
---|
105 | } |
---|
106 | } |
---|
107 | } |
---|
108 | } |
---|
109 | } |
---|