| 1 |
lars |
1 |
<?php
|
|
|
2 |
/*
|
|
|
3 |
* $Id$
|
|
|
4 |
*
|
|
|
5 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
6 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
7 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
8 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
9 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
10 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
11 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
12 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
13 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
14 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
15 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
16 |
*
|
|
|
17 |
* This software consists of voluntary contributions made by many individuals
|
|
|
18 |
* and is licensed under the LGPL. For more information, see
|
|
|
19 |
* <http://www.doctrine-project.org>.
|
|
|
20 |
*/
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* Builds result sets in to a scalar php array
|
|
|
24 |
*
|
|
|
25 |
* @package Doctrine
|
|
|
26 |
* @subpackage Hydrate
|
|
|
27 |
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
28 |
* @link www.doctrine-project.org
|
|
|
29 |
* @since 1.0
|
|
|
30 |
* @version $Revision$
|
|
|
31 |
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
|
|
32 |
*/
|
|
|
33 |
class Doctrine_Hydrator_ScalarDriver extends Doctrine_Hydrator_Abstract
|
|
|
34 |
{
|
|
|
35 |
public function hydrateResultSet($stmt)
|
|
|
36 |
{
|
|
|
37 |
$cache = array();
|
|
|
38 |
$result = array();
|
|
|
39 |
|
|
|
40 |
while ($data = $stmt->fetch(Doctrine_Core::FETCH_ASSOC)) {
|
|
|
41 |
$result[] = $this->_gatherRowData($data, $cache);
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
return $result;
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
protected function _gatherRowData($data, &$cache, $aliasPrefix = true)
|
|
|
48 |
{
|
|
|
49 |
$rowData = array();
|
|
|
50 |
foreach ($data as $key => $value) {
|
|
|
51 |
// Parse each column name only once. Cache the results.
|
|
|
52 |
if ( ! isset($cache[$key])) {
|
|
|
53 |
if ($key == 'DOCTRINE_ROWNUM') {
|
|
|
54 |
continue;
|
|
|
55 |
}
|
|
|
56 |
// cache general information like the column name <-> field name mapping
|
|
|
57 |
$e = explode('__', $key);
|
|
|
58 |
$columnName = strtolower(array_pop($e));
|
|
|
59 |
$cache[$key]['dqlAlias'] = $this->_tableAliases[strtolower(implode('__', $e))];
|
|
|
60 |
$table = $this->_queryComponents[$cache[$key]['dqlAlias']]['table'];
|
|
|
61 |
// check whether it's an aggregate value or a regular field
|
|
|
62 |
if (isset($this->_queryComponents[$cache[$key]['dqlAlias']]['agg'][$columnName])) {
|
|
|
63 |
$fieldName = $this->_queryComponents[$cache[$key]['dqlAlias']]['agg'][$columnName];
|
|
|
64 |
$cache[$key]['isAgg'] = true;
|
|
|
65 |
} else {
|
|
|
66 |
$fieldName = $table->getFieldName($columnName);
|
|
|
67 |
$cache[$key]['isAgg'] = false;
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
$cache[$key]['fieldName'] = $fieldName;
|
|
|
71 |
|
|
|
72 |
// cache type information
|
|
|
73 |
$type = $table->getTypeOfColumn($columnName);
|
|
|
74 |
if ($type == 'integer' || $type == 'string') {
|
|
|
75 |
$cache[$key]['isSimpleType'] = true;
|
|
|
76 |
} else {
|
|
|
77 |
$cache[$key]['type'] = $type;
|
|
|
78 |
$cache[$key]['isSimpleType'] = false;
|
|
|
79 |
}
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
$table = $this->_queryComponents[$cache[$key]['dqlAlias']]['table'];
|
|
|
83 |
$dqlAlias = $cache[$key]['dqlAlias'];
|
|
|
84 |
$fieldName = $cache[$key]['fieldName'];
|
|
|
85 |
|
|
|
86 |
$rowDataKey = $aliasPrefix ? $dqlAlias . '_' . $fieldName:$fieldName;
|
|
|
87 |
|
|
|
88 |
if ($cache[$key]['isSimpleType'] || $cache[$key]['isAgg']) {
|
|
|
89 |
$rowData[$rowDataKey] = $value;
|
|
|
90 |
} else {
|
|
|
91 |
$rowData[$rowDataKey] = $table->prepareValue(
|
|
|
92 |
$fieldName, $value, $cache[$key]['type']);
|
|
|
93 |
}
|
|
|
94 |
}
|
|
|
95 |
return $rowData;
|
|
|
96 |
}
|
|
|
97 |
}
|