From e77b65bbcf48455122cd0a38f8f50e2eccb5b7c8 Mon Sep 17 00:00:00 2001 From: zYne Date: Mon, 28 May 2007 19:03:57 +0000 Subject: [PATCH] first (ugly) draft for array population algorithm --- lib/Doctrine/Hydrate.php | 145 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 134 insertions(+), 11 deletions(-) diff --git a/lib/Doctrine/Hydrate.php b/lib/Doctrine/Hydrate.php index 829e0b33f..5413913f6 100644 --- a/lib/Doctrine/Hydrate.php +++ b/lib/Doctrine/Hydrate.php @@ -626,7 +626,7 @@ class Doctrine_Hydrate implements Serializable } $stmt = $this->_conn->execute($query, $params); - return (array) $this->parseData($stmt); + return $stmt; } /** * execute @@ -646,7 +646,8 @@ class Doctrine_Hydrate implements Serializable if ($cached === null) { // cache miss - $array = $this->_execute($params, $return); + $stmt = $this->_execute($params, $return); + $array = $this->parseData($stmt); $cached = $this->getCachedForm($array); @@ -673,11 +674,12 @@ class Doctrine_Hydrate implements Serializable $this->_aliasMap = $map; } } else { - $array = $this->_execute($params, $return); - } - - if ($return === Doctrine::FETCH_ARRAY) { - return $array; + $stmt = $this->_execute($params, $return); + if ($return === Doctrine::FETCH_ARRAY) { + return $this->parseData2($stmt); + } else { + $array = $this->parseData($stmt); + } } if (empty($this->_aliasMap)) { @@ -892,6 +894,127 @@ class Doctrine_Hydrate implements Serializable return $str; } + /** + * parseData + * parses the data returned by statement object + * + * @param mixed $stmt + * @return array + */ + public function parseData2($stmt) + { + $array = array(); + $cache = array(); + $rootMap = reset($this->_aliasMap); + $rootAlias = key($this->_aliasMap); + $index = 0; + $incr = true; + $lastAlias = ''; + $currData = array(); + + + while ($data = $stmt->fetch(PDO::FETCH_ASSOC)) { + foreach ($data as $key => $value) { + if ( ! isset($cache[$key])) { + $e = explode('__', $key); + $cache[$key]['field'] = $field = strtolower(array_pop($e)); + $componentAlias = $this->_tableAliases[strtolower(implode('__', $e))]; + + $cache[$key]['alias'] = $componentAlias; + + if (isset($this->_aliasMap[$componentAlias]['relation'])) { + $cache[$key]['component'] = $this->_aliasMap[$componentAlias]['relation']->getAlias(); + $cache[$key]['parent'] = $this->_aliasMap[$componentAlias]['parent']; + } else { + $cache[$key]['component'] = $this->_aliasMap[$componentAlias]['table']->getComponentName(); + } + + } + + $tmp = array(); + $alias = $cache[$key]['alias']; + $component = $cache[$key]['component']; + + + if ( ! isset($currData[$alias])) { + $currData[$alias] = array(); + } + if ( ! isset($prev[$alias])) { + $prev[$alias] = array(); + } + + if ($lastAlias !== $alias) { + // component changed + + if ($alias === $rootAlias) { + // dealing with root component + + if ( ! isset($prevData[$alias]) || $currData[$alias] !== $prevData[$alias]) { + if ( ! empty($currData[$alias])) { + + $array[$index] = $currData[$alias]; + $prev[$alias] =& $array[$index]; + $index++; + } + } + } else { + $parent = $cache[$key]['parent']; + $relation = $this->_aliasMap[$cache[$key]['alias']]['relation']; + + if ( ! isset($prevData[$alias]) || $currData[$alias] !== $prevData[$alias]) { + if ($relation->getType() >= Doctrine_Relation::MANY) { + $prev[$parent][$component][] = $currData[$alias]; + } else { + $prev[$parent][$component] = $currData[$alias]; + } + } + } + if (isset($currData[$alias])) { + $prevData[$alias] = $currData[$alias]; + } else { + $prevData[$alias] = array(); + } + + } else { + $field = $cache[$key]['field']; + $currData[$alias][$field] = $value; + } + + $lastAlias = $alias; + } + } + + foreach ($currData as $alias => $data) { + if ($alias === $rootAlias) { + // dealing with root component + + if ( ! isset($prevData[$alias]) || $currData[$alias] !== $prevData[$alias]) { + if ( ! empty($currData[$alias])) { + + $array[$index] = $currData[$alias]; + $prev[$alias] =& $array[$index]; + $index++; + } + } + } else { + $parent = $cache[$key]['parent']; + $relation = $this->_aliasMap[$cache[$key]['alias']]['relation']; + + if ( ! isset($prevData[$alias]) || $currData[$alias] !== $prevData[$alias]) { + if ($relation->getType() >= Doctrine_Relation::MANY) { + $prev[$parent][$component][] = $currData[$alias]; + } else { + $prev[$parent][$component] = $currData[$alias]; + } + } + } + } + + + $stmt->closeCursor(); + unset($cache); + return $array; + } /** * parseData * parses the data returned by statement object @@ -906,7 +1029,7 @@ class Doctrine_Hydrate implements Serializable while ($data = $stmt->fetch(PDO::FETCH_ASSOC)) { foreach ($data as $key => $value) { - if (!isset($cache[$key])) { + if ( ! isset($cache[$key])) { $e = explode('__', $key); $cache[$key]['field'] = strtolower(array_pop($e)); $cache[$key]['component'] = strtolower(implode('__', $e)); @@ -915,11 +1038,11 @@ class Doctrine_Hydrate implements Serializable $data[$cache[$key]['component']][$cache[$key]['field']] = $value; unset($data[$key]); - }; + } $array[] = $data; - }; + } $stmt->closeCursor(); - unset($cache); + return $array; } /**