source: pro-violet-viettel/sourcecode/application/libraries/Doctrine/ORM/Internal/Hydration/SimpleObjectHydrator.php @ 345

Last change on this file since 345 was 345, checked in by quyenla, 11 years ago

collaborator page

File size: 6.3 KB
Line 
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
20namespace Doctrine\ORM\Internal\Hydration;
21
22use \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
29class 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 ($sqlResult[$discrColumnName] === '') {
96                throw HydrationException::emptyDiscriminatorValue(key($this->_rsm->aliasMap));
97            }
98
99            $entityName = $this->class->discriminatorMap[$sqlResult[$discrColumnName]];
100
101            unset($sqlResult[$discrColumnName]);
102        }
103
104        foreach ($sqlResult as $column => $value) {
105            // Hydrate column information if not yet present
106            if ( ! isset($cache[$column])) {
107                if (($info = $this->hydrateColumnInfo($entityName, $column)) === null) {
108                    continue;
109                }
110
111                $cache[$column] = $info;
112            }
113
114            // Convert field to a valid PHP value
115            if (isset($cache[$column]['field'])) {
116                $type  = Type::getType($cache[$column]['class']->fieldMappings[$cache[$column]['name']]['type']);
117                $value = $type->convertToPHPValue($value, $this->_platform);
118            }
119
120            // Prevent overwrite in case of inherit classes using same property name (See AbstractHydrator)
121            if (isset($cache[$column]) && ( ! isset($data[$cache[$column]['name']]) || $value !== null)) {
122                $data[$cache[$column]['name']] = $value;
123            }
124        }
125
126        if (isset($this->_hints[Query::HINT_REFRESH_ENTITY])) {
127            $this->registerManaged($this->class, $this->_hints[Query::HINT_REFRESH_ENTITY], $data);
128        }
129
130        $uow    = $this->_em->getUnitOfWork();
131        $entity = $uow->createEntity($entityName, $data, $this->_hints);
132
133        $result[] = $entity;
134    }
135
136    /**
137     * Retrieve column information form ResultSetMapping.
138     *
139     * @param string $entityName
140     * @param string $column
141     *
142     * @return array
143     */
144    protected function hydrateColumnInfo($entityName, $column)
145    {
146        switch (true) {
147            case (isset($this->_rsm->fieldMappings[$column])):
148                $class = isset($this->declaringClasses[$column])
149                    ? $this->declaringClasses[$column]
150                    : $this->class;
151
152                // If class is not part of the inheritance, ignore
153                if ( ! ($class->name === $entityName || is_subclass_of($entityName, $class->name))) {
154                    return null;
155                }
156
157                return array(
158                    'class' => $class,
159                    'name'  => $this->_rsm->fieldMappings[$column],
160                    'field' => true,
161                );
162
163            case (isset($this->_rsm->relationMap[$column])):
164                $class = isset($this->_rsm->relationMap[$column])
165                    ? $this->_rsm->relationMap[$column]
166                    : $this->class;
167
168                // If class is not self referencing, ignore
169                if ( ! ($class === $entityName || is_subclass_of($entityName, $class))) {
170                    return null;
171                }
172
173                // TODO: Decide what to do with associations. It seems original code is incomplete.
174                // One solution is to load the association, but it might require extra efforts.
175                return array('name' => $column);
176
177            default:
178                return array(
179                    'name' => $this->_rsm->metaMappings[$column]
180                );
181        }
182    }
183}
Note: See TracBrowser for help on using the repository browser.