Process query only when needed.

This commit is contained in:
Ber Clausen 2013-12-21 09:21:27 -03:00
parent 422be67a03
commit 0adc062cfa
2 changed files with 16 additions and 16 deletions

View file

@ -318,10 +318,10 @@ class DataSource extends Object {
*
* @param string $query Query string needing replacements done.
* @param array $data Array of data with values that will be inserted in placeholders.
* @param string $association Name of association model being replaced
* @param Model $Model Model instance
* @param string $association Name of association model being replaced.
* @param Model $Model Model instance.
* @param array $stack
* @return string String of query data with placeholders replaced.
* @return mixed String of query data with placeholders replaced, or false on failure.
*/
public function insertQueryData($query, $data, $association, Model $Model, $stack) {
$keys = array('{$__cakeID__$}', '{$__cakeForeignKey__$}');

View file

@ -1307,20 +1307,20 @@ class DboSource extends DataSource {
foreach ($resultSet as &$row) {
if ($type === 'hasOne' || $type === 'belongsTo' || $type === 'hasMany') {
$query = $this->insertQueryData($queryTemplate, $row, $association, $Model, $stack);
$assocResultSet = array();
if ($query !== false) {
if (
($type === 'hasOne' || $type === 'belongsTo') &&
isset($row[$LinkModel->alias], $joined[$Model->alias]) &&
in_array($LinkModel->alias, $joined[$Model->alias])
) {
$joinedData = Hash::filter($row[$LinkModel->alias]);
if (!empty($joinedData)) {
$assocResultSet[0] = array($LinkModel->alias => $row[$LinkModel->alias]);
}
} else {
if (
($type === 'hasOne' || $type === 'belongsTo') &&
isset($row[$LinkModel->alias], $joined[$Model->alias]) &&
in_array($LinkModel->alias, $joined[$Model->alias])
) {
$joinedData = Hash::filter($row[$LinkModel->alias]);
if (!empty($joinedData)) {
$assocResultSet[0] = array($LinkModel->alias => $row[$LinkModel->alias]);
}
} else {
$query = $this->insertQueryData($queryTemplate, $row, $association, $Model, $stack);
if ($query !== false) {
$assocResultSet = $this->fetchAll($query, $Model->cacheQueries);
}
}