Skip to content
Permalink
Browse files

More tests passing

  • Loading branch information...
lorenzo committed Jan 29, 2014
1 parent bb2f6f6 commit fba9f17eebb66635417bcc23b6b24f0c306ed869
Showing with 31 additions and 23 deletions.
  1. +31 −23 src/ORM/EagerLoader.php
@@ -73,6 +73,10 @@ public function contain($associations = null, $override = false) {
return $this->_containments;
}
if (empty($associations)) {
return;
}
$associations = (array)$associations;
$current = current($associations);
if (is_array($current) && isset($current['instance'])) {
@@ -274,12 +278,7 @@ public function eagerLoad($query, $statement) {
}
$driver = $query->connection()->driver();
if (!($statement instanceof BufferedStatement)) {
$statement = new BufferedStatement($statement, $driver);
}
$collected = $this->_collectKeys($statement);
list($collected, $statement) = $this->_collectKeys($query, $statement);
foreach ($this->_loadEagerly as $meta) {
$contain = $meta['associations'];
$alias = $meta['instance']->source()->alias();
@@ -301,40 +300,49 @@ public function eagerLoad($query, $statement) {
* @param BufferedStatement $statement
* @return array
*/
protected function _collectKeys($statement) {
protected function _collectKeys($query, $statement) {
$collectKeys = [];
foreach ($this->_loadEagerly as $meta) {
$source = $meta['instance']->source();
if ($meta['instance']->requiresKeys($meta['config'])) {
$alias = $source->alias();
$pkFields = [];
foreach ((array)$source->primaryKey() as $key) {
$pkFields[] = key($this->aliasField($key, $alias));
$pkFields[] = key($query->aliasField($key, $alias));
}
$collectKeys[$alias] = [$alias, $pkFields, count($pkFields) === 1];
}
}
if (empty($collectKeys)) {
return [[], $statement];
}
if (!($statement instanceof BufferedStatement)) {
$statement = new BufferedStatement($statement, $query->connection()->driver());
}
return [$this->_groupKeys($statement, $collectKeys), $statement];
}
protected function _groupKeys($statement, $collectKeys) {
$keys = [];
if (!empty($collectKeys)) {
while ($result = $statement->fetch('assoc')) {
foreach ($collectKeys as $parts) {
if ($parts[2]) {
$keys[$parts[0]][] = $result[$parts[1][0]];
continue;
}
$collected = [];
foreach ($parts[1] as $key) {
$collected[] = $result[$key];
}
$keys[$parts[0]][] = $collected;
while ($result = $statement->fetch('assoc')) {
foreach ($collectKeys as $parts) {
if ($parts[2]) {
$keys[$parts[0]][] = $result[$parts[1][0]];
continue;
}
}
$statement->rewind();
$collected = [];
foreach ($parts[1] as $key) {
$collected[] = $result[$key];
}
$keys[$parts[0]][] = $collected;
}
}
$statement->rewind();
return $keys;
}

0 comments on commit fba9f17

Please sign in to comment.
You can’t perform that action at this time.