Skip to content

Commit

Permalink
Move variables to logical blocks.
Browse files Browse the repository at this point in the history
Unindent to ease readability, and avoid assigning variables when
unneeded.
Free a little memory before entering to recursive intensive loops.
  • Loading branch information
bar committed Nov 12, 2013
1 parent fdb4b11 commit f16695d
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions lib/Cake/Model/Datasource/DboSource.php
Expand Up @@ -1027,10 +1027,7 @@ public function create(Model $model, $fields = null, $values = null) {
public function read(Model $model, $queryData = array(), $recursive = null) {
$queryData = $this->_scrubQueryData($queryData);

$null = null;
$array = array('callbacks' => $queryData['callbacks']);
$linkedModels = array();
$bypass = false;

if ($recursive === null && isset($queryData['recursive'])) {
$recursive = $queryData['recursive'];
Expand All @@ -1041,6 +1038,7 @@ public function read(Model $model, $queryData = array(), $recursive = null) {
$model->recursive = $recursive;
}

$bypass = false;
if (!empty($queryData['fields'])) {
$bypass = true;
$queryData['fields'] = $this->fields($model, null, $queryData['fields']);
Expand All @@ -1057,30 +1055,34 @@ public function read(Model $model, $queryData = array(), $recursive = null) {
}

// Generate hasOne and belongsTo associations inside $queryData
$linkedModels = array();
$null = null;
foreach ($_associations as $type) {
if ($type !== 'hasOne' && $type !== 'belongsTo') {
continue;
}

foreach ($model->{$type} as $assoc => $assocData) {
$linkModel = $model->{$assoc};
$external = isset($assocData['external']);

if ($model->useDbConfig === $linkModel->useDbConfig) {
if ($bypass) {
$assocData['fields'] = false;
}
if ($this->generateAssociationQuery($model, $linkModel, $type, $assoc, $assocData, $queryData, $external, $null) === true) {
$linkedModels[$type . '/' . $assoc] = true;
}
if ($model->useDbConfig !== $linkModel->useDbConfig) {
continue;
}

$external = isset($assocData['external']);
if ($bypass) {
$assocData['fields'] = false;
}
if ($this->generateAssociationQuery($model, $linkModel, $type, $assoc, $assocData, $queryData, $external, $null) === true) {
$linkedModels[$type . '/' . $assoc] = true;
}
}
}

// Build SQL statement with the primary model, plus hasOne and belongsTo associations
$query = $this->generateAssociationQuery($model, null, null, null, null, $queryData, false, $null);

$resultSet = $this->fetchAll($query, $model->cacheQueries);
unset($query);

if ($resultSet === false) {
$model->onError();
Expand Down

0 comments on commit f16695d

Please sign in to comment.