Completed
Push — master ( 5eebdc...009e94 )
by Marco
20:26
created

SimpleObjectHydrator::hydrateAllData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
c 0
b 0
f 0
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 6
nc 2
nop 0
crap 2
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
use Doctrine\ORM\Mapping\ClassMetadata;
24
use Doctrine\ORM\Query;
25
26
class SimpleObjectHydrator extends AbstractHydrator
27
{
28
    /**
29
     * @var ClassMetadata
30
     */
31
    private $class;
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 419
    protected function prepare()
37
    {
38 419
        if (count($this->_rsm->aliasMap) !== 1) {
39
            throw new \RuntimeException("Cannot use SimpleObjectHydrator with a ResultSetMapping that contains more than one object result.");
40
        }
41
42 419
        if ($this->_rsm->scalarMappings) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->_rsm->scalarMappings of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
43
            throw new \RuntimeException("Cannot use SimpleObjectHydrator with a ResultSetMapping that contains scalar mappings.");
44
        }
45
46 419
        $this->class = $this->getClassMetadata(reset($this->_rsm->aliasMap));
47 419
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 416
    protected function cleanup()
53
    {
54 416
        parent::cleanup();
55
56 416
        $this->_uow->triggerEagerLoads();
57 416
        $this->_uow->hydrationComplete();
58 416
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 418
    protected function hydrateAllData()
64
    {
65 418
        $result = array();
66
67 418
        while ($row = $this->_stmt->fetch(PDO::FETCH_ASSOC)) {
68 363
            $this->hydrateRowData($row, $result);
69
        }
70
71 416
        $this->_em->getUnitOfWork()->triggerEagerLoads();
72
73 416
        return $result;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 364
    protected function hydrateRowData(array $sqlResult, array &$result)
80
    {
81 364
        $entityName = $this->class->name;
82 364
        $data       = array();
83
84
        // We need to find the correct entity class name if we have inheritance in resultset
85 364
        if ($this->class->inheritanceType !== ClassMetadata::INHERITANCE_TYPE_NONE) {
86 73
            $discrColumnName = $this->_platform->getSQLResultCasing($this->class->discriminatorColumn['name']);
87
88
            // Find mapped discriminator column from the result set.
89 73
            if ($metaMappingDiscrColumnName = array_search($discrColumnName, $this->_rsm->metaMappings)) {
90 73
                $discrColumnName = $metaMappingDiscrColumnName;
91
            }
92
93 73
            if ( ! isset($sqlResult[$discrColumnName])) {
94 1
                throw HydrationException::missingDiscriminatorColumn($entityName, $discrColumnName, key($this->_rsm->aliasMap));
95
            }
96
97 72
            if ($sqlResult[$discrColumnName] === '') {
98
                throw HydrationException::emptyDiscriminatorValue(key($this->_rsm->aliasMap));
99
            }
100
101 72
            $discrMap = $this->class->discriminatorMap;
102
103 72
            if ( ! isset($discrMap[$sqlResult[$discrColumnName]])) {
104 1
                throw HydrationException::invalidDiscriminatorValue($sqlResult[$discrColumnName], array_keys($discrMap));
105
            }
106
107 71
            $entityName = $discrMap[$sqlResult[$discrColumnName]];
108
109 71
            unset($sqlResult[$discrColumnName]);
110
        }
111
112 362
        foreach ($sqlResult as $column => $value) {
113
            // An ObjectHydrator should be used instead of SimpleObjectHydrator
114 362
            if (isset($this->_rsm->relationMap[$column])) {
115
                throw new \Exception(sprintf('Unable to retrieve association information for column "%s"', $column));
116
            }
117
118 362
            $cacheKeyInfo = $this->hydrateColumnInfo($column);
119
120 362
            if ( ! $cacheKeyInfo) {
121 1
                continue;
122
            }
123
124
            // Check if value is null before conversion (because some types convert null to something else)
125 362
            $valueIsNull = null === $value;
126
127
            // Convert field to a valid PHP value
128 362
            if (isset($cacheKeyInfo['type'])) {
129 362
                $type  = $cacheKeyInfo['type'];
130 362
                $value = $type->convertToPHPValue($value, $this->_platform);
131
            }
132
133 362
            $fieldName = $cacheKeyInfo['fieldName'];
134
135
            // Prevent overwrite in case of inherit classes using same property name (See AbstractHydrator)
136 362
            if ( ! isset($data[$fieldName]) || ! $valueIsNull) {
137 362
                $data[$fieldName] = $value;
138
            }
139
        }
140
141 362
        if (isset($this->_hints[Query::HINT_REFRESH_ENTITY])) {
142 47
            $this->registerManaged($this->class, $this->_hints[Query::HINT_REFRESH_ENTITY], $data);
143
        }
144
145 362
        $uow    = $this->_em->getUnitOfWork();
146 362
        $entity = $uow->createEntity($entityName, $data, $this->_hints);
147
148 362
        $result[] = $entity;
149
150 362
        if (isset($this->_hints[Query::HINT_INTERNAL_ITERATION]) && $this->_hints[Query::HINT_INTERNAL_ITERATION]) {
151 1
            $this->_uow->hydrationComplete();
152
        }
153 362
    }
154
}
155