Skip to content

Commit

Permalink
Explicitly build SQL statements, calling buildAssociationQuery().
Browse files Browse the repository at this point in the history
  • Loading branch information
bar committed Nov 12, 2013
1 parent f0fcaa1 commit b35c967
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 34 deletions.
17 changes: 9 additions & 8 deletions lib/Cake/Model/Datasource/DboSource.php
Expand Up @@ -1068,18 +1068,20 @@ public function read(Model $model, $queryData = array(), $recursive = null) {
continue;
}

$external = isset($assocData['external']);
if ($bypass) {
$assocData['fields'] = false;
}
$external = isset($assocData['external']);

if ($this->generateAssociationQuery($model, $linkModel, $type, $assoc, $assocData, $queryData, $external) === 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);
$query = $this->buildAssociationQuery($model, $queryData);

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

Expand All @@ -1095,6 +1097,7 @@ public function read(Model $model, $queryData = array(), $recursive = null) {
$filtered = $this->_filterResults($resultSet, $model);
}

// Deep associations
if ($model->recursive > -1) {
$joined = array();
if (isset($queryData['joins'][0]['alias'])) {
Expand Down Expand Up @@ -1127,6 +1130,7 @@ public function read(Model $model, $queryData = array(), $recursive = null) {
}
}
}

if ($queryData['callbacks'] === true || $queryData['callbacks'] === 'after') {
$this->_filterResults($resultSet, $model, $filtered);
}
Expand All @@ -1140,7 +1144,9 @@ public function read(Model $model, $queryData = array(), $recursive = null) {
}

/**
* Passes association results thru afterFind filters of corresponding model
* Passes association results through afterFind filters of corresponding model.
*
* The primary model is always filtered.
*
* @param array $results Reference of resultset to be filtered
* @param Model $model Instance of model to operate against
Expand Down Expand Up @@ -1570,14 +1576,9 @@ public function buildAssociationQuery(Model $Model, &$queryData) {
* @param array $queryData
* @param boolean $external Whether or not the association query is on an external datasource.
* @return mixed
* String containing an SQL statement, when $linkModel is null.
* True. when $external is false and association $type is 'hasOne' or 'belongsTo'.
*/
public function generateAssociationQuery(Model $Model, $linkModel, $type, $association, $assocData, &$queryData, $external) {
if ($linkModel === null) {
return $this->buildAssociationQuery($Model, $queryData);
}

$assocData = $this->_scrubQueryData($assocData);

if ($external && !empty($assocData['finderQuery'])) {
Expand Down

0 comments on commit b35c967

Please sign in to comment.