|
0
|
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 |
|
|
|
21 |
namespace Doctrine\ORM\Internal\Hydration; |
|
|
22 |
|
|
|
23 |
use \PDO; |
|
|
24 |
use Doctrine\ORM\Mapping\ClassMetadata; |
|
|
25 |
use Doctrine\DBAL\Types\Type; |
|
|
26 |
use Doctrine\ORM\Query; |
|
|
27 |
|
|
|
28 |
class SimpleObjectHydrator extends AbstractHydrator |
|
|
29 |
{ |
|
|
30 |
/** |
|
|
31 |
* @var ClassMetadata |
|
|
32 |
*/ |
|
|
33 |
private $class; |
|
|
34 |
|
|
|
35 |
private $declaringClasses = array(); |
|
|
36 |
|
|
|
37 |
protected function _hydrateAll() |
|
|
38 |
{ |
|
|
39 |
$result = array(); |
|
|
40 |
$cache = array(); |
|
|
41 |
|
|
|
42 |
while ($row = $this->_stmt->fetch(PDO::FETCH_ASSOC)) { |
|
|
43 |
$this->_hydrateRow($row, $cache, $result); |
|
|
44 |
} |
|
|
45 |
|
|
|
46 |
$this->_em->getUnitOfWork()->triggerEagerLoads(); |
|
|
47 |
|
|
|
48 |
return $result; |
|
|
49 |
} |
|
|
50 |
|
|
|
51 |
protected function _prepare() |
|
|
52 |
{ |
|
|
53 |
if (count($this->_rsm->aliasMap) == 1) { |
|
|
54 |
$this->class = $this->_em->getClassMetadata(reset($this->_rsm->aliasMap)); |
|
|
55 |
if ($this->class->inheritanceType !== ClassMetadata::INHERITANCE_TYPE_NONE) { |
|
|
56 |
foreach ($this->_rsm->declaringClasses AS $column => $class) { |
|
|
57 |
$this->declaringClasses[$column] = $this->_em->getClassMetadata($class); |
|
|
58 |
} |
|
|
59 |
} |
|
|
60 |
} else { |
|
|
61 |
throw new \RuntimeException("Cannot use SimpleObjectHydrator with a ResultSetMapping not containing exactly one object result."); |
|
|
62 |
} |
|
|
63 |
if ($this->_rsm->scalarMappings) { |
|
|
64 |
throw new \RuntimeException("Cannot use SimpleObjectHydrator with a ResultSetMapping that contains scalar mappings."); |
|
|
65 |
} |
|
|
66 |
} |
|
|
67 |
|
|
|
68 |
protected function _hydrateRow(array $sqlResult, array &$cache, array &$result) |
|
|
69 |
{ |
|
|
70 |
$data = array(); |
|
|
71 |
if ($this->class->inheritanceType == ClassMetadata::INHERITANCE_TYPE_NONE) { |
|
|
72 |
foreach ($sqlResult as $column => $value) { |
|
|
73 |
|
|
|
74 |
if (!isset($cache[$column])) { |
|
|
75 |
if (isset($this->_rsm->fieldMappings[$column])) { |
|
|
76 |
$cache[$column]['name'] = $this->_rsm->fieldMappings[$column]; |
|
|
77 |
$cache[$column]['field'] = true; |
|
|
78 |
} else { |
|
|
79 |
$cache[$column]['name'] = $this->_rsm->metaMappings[$column]; |
|
|
80 |
} |
|
|
81 |
} |
|
|
82 |
|
|
|
83 |
if (isset($cache[$column]['field'])) { |
|
|
84 |
$value = Type::getType($this->class->fieldMappings[$cache[$column]['name']]['type']) |
|
|
85 |
->convertToPHPValue($value, $this->_platform); |
|
|
86 |
} |
|
|
87 |
$data[$cache[$column]['name']] = $value; |
|
|
88 |
} |
|
|
89 |
$entityName = $this->class->name; |
|
|
90 |
} else { |
|
|
91 |
$discrColumnName = $this->_platform->getSQLResultCasing($this->class->discriminatorColumn['name']); |
|
|
92 |
$entityName = $this->class->discriminatorMap[$sqlResult[$discrColumnName]]; |
|
|
93 |
unset($sqlResult[$discrColumnName]); |
|
|
94 |
foreach ($sqlResult as $column => $value) { |
|
|
95 |
if (!isset($cache[$column])) { |
|
|
96 |
if (isset($this->_rsm->fieldMappings[$column])) { |
|
|
97 |
$field = $this->_rsm->fieldMappings[$column]; |
|
|
98 |
$class = $this->declaringClasses[$column]; |
|
|
99 |
if ($class->name == $entityName || is_subclass_of($entityName, $class->name)) { |
|
|
100 |
$cache[$column]['name'] = $field; |
|
|
101 |
$cache[$column]['class'] = $class; |
|
|
102 |
} |
|
|
103 |
} else if (isset($this->_rsm->relationMap[$column])) { |
|
|
104 |
if ($this->_rsm->relationMap[$column] == $entityName || is_subclass_of($entityName, $this->_rsm->relationMap[$column])) { |
|
|
105 |
$cache[$column]['name'] = $field; |
|
|
106 |
} |
|
|
107 |
} else { |
|
|
108 |
$cache[$column]['name'] = $this->_rsm->metaMappings[$column]; |
|
|
109 |
} |
|
|
110 |
} |
|
|
111 |
|
|
|
112 |
if (isset($cache[$column]['class'])) { |
|
|
113 |
$value = Type::getType($cache[$column]['class']->fieldMappings[$cache[$column]['name']]['type']) |
|
|
114 |
->convertToPHPValue($value, $this->_platform); |
|
|
115 |
} |
|
|
116 |
|
|
|
117 |
// the second and part is to prevent overwrites in case of multiple |
|
|
118 |
// inheritance classes using the same property name (See AbstractHydrator) |
|
|
119 |
if (isset($cache[$column]) && (!isset($data[$cache[$column]['name']]) || $value !== null)) { |
|
|
120 |
$data[$cache[$column]['name']] = $value; |
|
|
121 |
} |
|
|
122 |
} |
|
|
123 |
} |
|
|
124 |
|
|
|
125 |
if (isset($this->_hints[Query::HINT_REFRESH_ENTITY])) { |
|
|
126 |
$this->registerManaged($this->class, $this->_hints[Query::HINT_REFRESH_ENTITY], $data); |
|
|
127 |
} |
|
|
128 |
|
|
|
129 |
$result[] = $this->_em->getUnitOfWork()->createEntity($entityName, $data, $this->_hints); |
|
|
130 |
} |
|
|
131 |
} |