Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / lib / Doctrine / ORM / Internal / Hydration / SimpleObjectHydrator.php
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
20 namespace Doctrine\ORM\Internal\Hydration;
21
22 use \PDO,
23     Doctrine\DBAL\Types\Type,
24     Doctrine\ORM\Mapping\ClassMetadata,
25     Doctrine\ORM\Event\LifecycleEventArgs,
26     Doctrine\ORM\Events,
27     Doctrine\ORM\Query;
28
29 class SimpleObjectHydrator extends AbstractHydrator
30 {
31     /**
32      * @var ClassMetadata
33      */
34     private $class;
35
36     /**
37      * @var array
38      */
39     private $declaringClasses = array();
40
41     /**
42      * {@inheritdoc}
43      */
44     protected function hydrateAllData()
45     {
46         $result = array();
47         $cache = array();
48
49         while ($row = $this->_stmt->fetch(PDO::FETCH_ASSOC)) {
50             $this->hydrateRowData($row, $cache, $result);
51         }
52
53         $this->_em->getUnitOfWork()->triggerEagerLoads();
54
55         return $result;
56     }
57
58     /**
59      * {@inheritdoc}
60      */
61     protected function prepare()
62     {
63         if (count($this->_rsm->aliasMap) !== 1) {
64             throw new \RuntimeException("Cannot use SimpleObjectHydrator with a ResultSetMapping that contains more than one object result.");
65         }
66
67         if ($this->_rsm->scalarMappings) {
68             throw new \RuntimeException("Cannot use SimpleObjectHydrator with a ResultSetMapping that contains scalar mappings.");
69         }
70
71         $this->class = $this->_em->getClassMetadata(reset($this->_rsm->aliasMap));
72
73         // We only need to add declaring classes if we have inheritance.
74         if ($this->class->inheritanceType === ClassMetadata::INHERITANCE_TYPE_NONE) {
75             return;
76         }
77
78         foreach ($this->_rsm->declaringClasses as $column => $class) {
79             $this->declaringClasses[$column] = $this->_em->getClassMetadata($class);
80         }
81     }
82
83     /**
84      * {@inheritdoc}
85      */
86     protected function hydrateRowData(array $sqlResult, array &$cache, array &$result)
87     {
88         $entityName = $this->class->name;
89         $data       = array();
90
91         // We need to find the correct entity class name if we have inheritance in resultset
92         if ($this->class->inheritanceType !== ClassMetadata::INHERITANCE_TYPE_NONE) {
93             $discrColumnName = $this->_platform->getSQLResultCasing($this->class->discriminatorColumn['name']);
94
95             if ( ! isset($sqlResult[$discrColumnName])) {
96                 throw HydrationException::missingDiscriminatorColumn($entityName, $discrColumnName, key($this->_rsm->aliasMap));
97             }
98
99             if ($sqlResult[$discrColumnName] === '') {
100                 throw HydrationException::emptyDiscriminatorValue(key($this->_rsm->aliasMap));
101             }
102
103             $entityName = $this->class->discriminatorMap[$sqlResult[$discrColumnName]];
104
105             unset($sqlResult[$discrColumnName]);
106         }
107
108         foreach ($sqlResult as $column => $value) {
109             // Hydrate column information if not yet present
110             if ( ! isset($cache[$column])) {
111                 if (($info = $this->hydrateColumnInfo($entityName, $column)) === null) {
112                     continue;
113                 }
114
115                 $cache[$column] = $info;
116             }
117
118             // Convert field to a valid PHP value
119             if (isset($cache[$column]['field'])) {
120                 $type  = Type::getType($cache[$column]['class']->fieldMappings[$cache[$column]['name']]['type']);
121                 $value = $type->convertToPHPValue($value, $this->_platform);
122             }
123
124             // Prevent overwrite in case of inherit classes using same property name (See AbstractHydrator)
125             if (isset($cache[$column]) && ( ! isset($data[$cache[$column]['name']]) || $value !== null)) {
126                 $data[$cache[$column]['name']] = $value;
127             }
128         }
129
130         if (isset($this->_hints[Query::HINT_REFRESH_ENTITY])) {
131             $this->registerManaged($this->class, $this->_hints[Query::HINT_REFRESH_ENTITY], $data);
132         }
133
134         $uow    = $this->_em->getUnitOfWork();
135         $entity = $uow->createEntity($entityName, $data, $this->_hints);
136
137         $result[] = $entity;
138     }
139
140     /**
141      * Retrieve column information form ResultSetMapping.
142      *
143      * @param string $entityName
144      * @param string $column
145      *
146      * @return array
147      */
148     protected function hydrateColumnInfo($entityName, $column)
149     {
150         switch (true) {
151             case (isset($this->_rsm->fieldMappings[$column])):
152                 $class = isset($this->declaringClasses[$column])
153                     ? $this->declaringClasses[$column]
154                     : $this->class;
155
156                 // If class is not part of the inheritance, ignore
157                 if ( ! ($class->name === $entityName || is_subclass_of($entityName, $class->name))) {
158                     return null;
159                 }
160
161                 return array(
162                     'class' => $class,
163                     'name'  => $this->_rsm->fieldMappings[$column],
164                     'field' => true,
165                 );
166
167             case (isset($this->_rsm->relationMap[$column])):
168                 $class = isset($this->_rsm->relationMap[$column])
169                     ? $this->_rsm->relationMap[$column]
170                     : $this->class;
171
172                 // If class is not self referencing, ignore
173                 if ( ! ($class === $entityName || is_subclass_of($entityName, $class))) {
174                     return null;
175                 }
176
177                 // TODO: Decide what to do with associations. It seems original code is incomplete.
178                 // One solution is to load the association, but it might require extra efforts.
179                 return array('name' => $column);
180
181             default:
182                 return array(
183                     'name' => $this->_rsm->metaMappings[$column]
184                 );
185         }
186     }
187 }