Skip to content

Commit

Permalink
Some optimizations in queryAssociation.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrbasso committed Feb 26, 2011
1 parent cbe8d9c commit ef9dfe7
Showing 1 changed file with 14 additions and 20 deletions.
34 changes: 14 additions & 20 deletions cake/libs/model/datasources/dbo_source.php
Expand Up @@ -1189,8 +1189,8 @@ public function queryAssociation($model, &$linkModel, $type, $association, $asso

if ($type === 'hasMany' && empty($assocData['limit']) && !empty($assocData['foreignKey'])) {
$ins = $fetch = array();
for ($i = 0; $i < $count; $i++) {
if ($in = $this->insertQueryData('{$__cakeID__$}', $resultSet[$i], $association, $assocData, $model, $linkModel, $stack)) {
foreach ($resultSet as &$result) {
if ($in = $this->insertQueryData('{$__cakeID__$}', $result, $association, $assocData, $model, $linkModel, $stack)) {
$ins[] = $in;
}
}
Expand Down Expand Up @@ -1222,8 +1222,8 @@ public function queryAssociation($model, &$linkModel, $type, $association, $asso
return $this->__mergeHasMany($resultSet, $fetch, $association, $model, $linkModel, $recursive);
} elseif ($type === 'hasAndBelongsToMany') {
$ins = $fetch = array();
for ($i = 0; $i < $count; $i++) {
if ($in = $this->insertQueryData('{$__cakeID__$}', $resultSet[$i], $association, $assocData, $model, $linkModel, $stack)) {
foreach ($resultSet as &$result) {
if ($in = $this->insertQueryData('{$__cakeID__$}', $result, $association, $assocData, $model, $linkModel, $stack)) {
$ins[] = $in;
}
}
Expand Down Expand Up @@ -1252,30 +1252,24 @@ public function queryAssociation($model, &$linkModel, $type, $association, $asso
}
}

for ($i = 0; $i < $count; $i++) {
$row =& $resultSet[$i];

foreach ($resultSet as &$row) {
if ($type !== 'hasAndBelongsToMany') {
$q = $this->insertQueryData($query, $resultSet[$i], $association, $assocData, $model, $linkModel, $stack);
$q = $this->insertQueryData($query, $row, $association, $assocData, $model, $linkModel, $stack);
if ($q !== false) {
$fetch = $this->fetchAll($q, $model->cacheQueries);
} else {
$fetch = null;
}
}
$selfJoin = false;

if ($linkModel->name === $model->name) {
$selfJoin = true;
}
$selfJoin = $linkModel->name === $model->name;

if (!empty($fetch) && is_array($fetch)) {
if ($recursive > 0) {
foreach ($linkModel->associations() as $type1) {
foreach ($linkModel->{$type1} as $assoc1 => $assocData1) {
$deepModel = $linkModel->{$assoc1};

if (($type1 === 'belongsTo') || ($deepModel->alias === $model->alias && $type === 'belongsTo') || ($deepModel->alias != $model->alias)) {
if ($type1 === 'belongsTo' || ($deepModel->alias === $model->alias && $type === 'belongsTo') || ($deepModel->alias != $model->alias)) {
$tmpStack = $stack;
$tmpStack[] = $assoc1;
if ($linkModel->useDbConfig == $deepModel->useDbConfig) {
Expand All @@ -1288,7 +1282,7 @@ public function queryAssociation($model, &$linkModel, $type, $association, $asso
}
}
}
if ($type == 'hasAndBelongsToMany') {
if ($type === 'hasAndBelongsToMany') {
$uniqueIds = $merge = array();

foreach ($fetch as $j => $data) {
Expand All @@ -1302,17 +1296,17 @@ public function queryAssociation($model, &$linkModel, $type, $association, $asso
if (empty($merge) && !isset($row[$association])) {
$row[$association] = $merge;
} else {
$this->__mergeAssociation($resultSet[$i], $merge, $association, $type);
$this->__mergeAssociation($row, $merge, $association, $type);
}
} else {
$this->__mergeAssociation($resultSet[$i], $fetch, $association, $type, $selfJoin);
$this->__mergeAssociation($row, $fetch, $association, $type, $selfJoin);
}
if (isset($resultSet[$i][$association])) {
$resultSet[$i][$association] = $linkModel->afterFind($resultSet[$i][$association], false);
if (isset($row[$association])) {
$row[$association] = $linkModel->afterFind($row[$association], false);
}
} else {
$tempArray[0][$association] = false;
$this->__mergeAssociation($resultSet[$i], $tempArray, $association, $type, $selfJoin);
$this->__mergeAssociation($row, $tempArray, $association, $type, $selfJoin);
}
}
}
Expand Down

0 comments on commit ef9dfe7

Please sign in to comment.