Merging fixes between branches for DboSource::generateAssociationQuery()

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@4037 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
phpnut 2006-11-30 22:26:24 +00:00
parent 32d3349eb2
commit 83afee9a52

View file

@ -548,13 +548,14 @@ class DboSource extends DataSource {
if ($model->recursive > -1) { if ($model->recursive > -1) {
$linkModel =& $model->{$assoc}; $linkModel =& $model->{$assoc};
$external = isset($assocData['external']);
if ($model->name == $linkModel->name && $type != 'hasAndBelongsToMany' && $type != 'hasMany') { if ($model->name == $linkModel->name && $type != 'hasAndBelongsToMany' && $type != 'hasMany') {
if (true === $this->generateSelfAssociationQuery($model, $linkModel, $type, $assoc, $assocData, $queryData, false, $null)) { if (true === $this->generateSelfAssociationQuery($model, $linkModel, $type, $assoc, $assocData, $queryData, $external, $null)) {
$linkedModels[] = $type . '/' . $assoc; $linkedModels[] = $type . '/' . $assoc;
} }
} else { } else {
if ($model->useDbConfig == $linkModel->useDbConfig) { if ($model->useDbConfig == $linkModel->useDbConfig) {
if (true === $this->generateAssociationQuery($model, $linkModel, $type, $assoc, $assocData, $queryData, false, $null)) { if (true === $this->generateAssociationQuery($model, $linkModel, $type, $assoc, $assocData, $queryData, $external, $null)) {
$linkedModels[] = $type . '/' . $assoc; $linkedModels[] = $type . '/' . $assoc;
} }
} }
@ -868,23 +869,35 @@ class DboSource extends DataSource {
if ($model->name == $linkModel->name) { if ($model->name == $linkModel->name) {
$joinedOnSelf = true; $joinedOnSelf = true;
} }
if (is_string($queryData['conditions'])) {
$queryData['conditions'] = array($queryData['conditions']); if ($external && isset($assocData['finderQuery'])) {
if (!empty($assocData['finderQuery']) && $assocData['finderQuery'] != null) {
return $assocData['finderQuery'];
}
}
if (!$external && in_array($type, array('hasOne', 'belongsTo'))) {
if ($this->__bypass === false) {
$fields = join(', ', $this->fields($linkModel, $alias, $assocData['fields']));
$this->__assocJoins['fields'][] = $fields;
} else {
$this->__assocJoins = null;
}
}
$limit = '';
if (isset($assocData['limit'])) {
if (!isset($assocData['offset']) && isset($assocData['page'])) {
$assocData['offset'] = ($assocData['page'] - 1) * $assocData['limit'];
} elseif (!isset($assocData['offset'])) {
$assocData['offset'] = null;
}
$limit = $this->limit($assocData['limit'], $assocData['offset']);
} }
switch($type) { switch($type) {
case 'hasOne': case 'hasOne':
if ($external || isset($assocData['external'])) { if ($external) {
if (isset($assocData['finderQuery'])) {
return $assocData['finderQuery'];
}
if (!isset($assocData['fields'])) {
$assocData['fields'] = '';
}
$limit = '';
if (isset($queryData['limit']) && !empty($queryData['limit'])) { if (isset($queryData['limit']) && !empty($queryData['limit'])) {
$limit = $this->limit($queryData['limit'], $queryData['offset']); $limit = $this->limit($queryData['limit'], $queryData['offset']);
} }
@ -910,7 +923,6 @@ class DboSource extends DataSource {
if (trim($conditions) != '') { if (trim($conditions) != '') {
$conditions .= ' AND '; $conditions .= ' AND ';
} }
$conditions .= $cond; $conditions .= $cond;
} }
@ -921,33 +933,17 @@ class DboSource extends DataSource {
return $sql; return $sql;
} else { } else {
if (!isset($assocData['fields'])) {
$assocData['fields'] = '';
}
if ($this->__bypass === false) {
$fields = join(', ', $this->fields($linkModel, $alias, $assocData['fields']));
$this->__assocJoins['fields'][] = $fields;
} else {
$this->__assocJoins = null;
}
$sql = ' LEFT JOIN ' . $this->fullTableName($linkModel); $sql = ' LEFT JOIN ' . $this->fullTableName($linkModel);
$sql .= ' ' . $this->alias . $this->name($alias) . ' ON ' . $this->name($alias) . '.'; $sql .= ' ' . $this->alias . $this->name($alias) . ' ON ';
$sql .= $this->name($assocData['foreignKey']) . ' = ' . $model->escapeField($model->primaryKey); $sql .= $this->name($alias) . '.' . $this->name($assocData['foreignKey']);
$sql .= ' = ' . $model->escapeField($model->primaryKey);
if ($assocData['order'] != null) { if ($assocData['order'] != null) {
$queryData['order'][] = $assocData['order']; $queryData['order'][] = $assocData['order'];
} }
if (isset($assocData['conditions']) && !empty($assocData['conditions'])) { $this->__mergeConditions($queryData, $assocData);
if (is_array($queryData['conditions'])) {
$queryData['conditions'] = array_merge($assocData['conditions'], $queryData['conditions']);
} else {
$queryData['conditions'] = $assocData['conditions'];
}
}
if (!in_array($sql, $queryData['joins'])) { if (!in_array($sql, $queryData['joins'])) {
$queryData['joins'][] = $sql; $queryData['joins'][] = $sql;
} }
@ -955,15 +951,7 @@ class DboSource extends DataSource {
} }
break; break;
case 'belongsTo': case 'belongsTo':
if ($external || isset($assocData['external'])) { if ($external) {
$limit = '';
if (isset($assocData['limit'])) {
$limit = $this->limit($assocData['limit'], $queryData['offset']);
}
if (!isset($assocData['fields'])) {
$assocData['fields'] = '';
}
$sql = 'SELECT '; $sql = 'SELECT ';
if ($this->goofyLimit) { if ($this->goofyLimit) {
@ -978,7 +966,7 @@ class DboSource extends DataSource {
$condition .= ' = {$__cakeForeignKey__$}'; $condition .= ' = {$__cakeForeignKey__$}';
if (is_array($conditions)) { if (is_array($conditions)) {
$conditions[] = $condition; $conditions[] = $condition;
} else { } else {
if (trim($conditions) != '') { if (trim($conditions) != '') {
$conditions .= ' AND '; $conditions .= ' AND ';
@ -994,39 +982,13 @@ class DboSource extends DataSource {
return $sql; return $sql;
} else { } else {
if (!isset($assocData['fields'])) {
$assocData['fields'] = '';
}
if ($this->__bypass === false) {
$fields = join(', ', $this->fields($linkModel, $alias, $assocData['fields']));
$this->__assocJoins['fields'][] = $fields;
} else {
$this->__assocJoins = null;
}
$sql = ' LEFT JOIN ' . $this->fullTableName($linkModel); $sql = ' LEFT JOIN ' . $this->fullTableName($linkModel);
$sql .= ' ' . $this->alias . $this->name($alias) . ' ON '; $sql .= ' ' . $this->alias . $this->name($alias) . ' ON ';
$sql .= $this->name($model->name) . '.' . $this->name($assocData['foreignKey']); $sql .= $this->name($model->name) . '.' . $this->name($assocData['foreignKey']);
$sql .= ' = ' . $this->name($alias) . '.' . $this->name($linkModel->primaryKey); $sql .= ' = ' . $this->name($alias) . '.' . $this->name($linkModel->primaryKey);
if (isset($assocData['conditions']) && !empty($assocData['conditions'])) { $this->__mergeConditions($queryData, $assocData);
if (is_array($queryData['conditions'])) {
$queryData['conditions'] = array_merge((array)$assocData['conditions'], $queryData['conditions']);
} else {
if (!empty($queryData['conditions'])){
$queryData['conditions'] = array($queryData['conditions']);
if (is_array($assocData['conditions'])){
array_merge($queryData['conditions'],$assocData['conditions']);
} else {
$queryData['conditions'][] = $assocData['conditions'];
}
} else {
$queryData['conditions'] = $assocData['conditions'];
}
}
}
if (!in_array($sql, $queryData['joins'])) { if (!in_array($sql, $queryData['joins'])) {
$queryData['joins'][] = $sql; $queryData['joins'][] = $sql;
} }
@ -1035,105 +997,74 @@ class DboSource extends DataSource {
break; break;
case 'hasMany': case 'hasMany':
if (isset($assocData['finderQuery']) && $assocData['finderQuery'] != null) {
$sql = $assocData['finderQuery']; $conditions = $assocData['conditions'];
$sql = 'SELECT ';
if ($this->goofyLimit) {
$sql .= $limit;
}
$sql .= ' ' . join(', ', $this->fields($linkModel, $alias, $assocData['fields']));
$sql .= ' FROM ' . $this->fullTableName($linkModel) . ' ' . $this->alias . $this->name($alias);
if (is_array($conditions)) {
$conditions[$alias . '.' . $assocData['foreignKey']] = '{$__cakeID__$}';
} else { } else {
$cond = $this->name($alias) . '.' . $this->name($assocData['foreignKey']);
$limit = ''; $cond .= ' = {$__cakeID__$}';
if (isset($assocData['limit'])) { if (trim($conditions) != '') {
if (!isset($assocData['offset']) && isset($assocData['page'])) { $conditions .= ' AND ';
$assocData['offset'] = ($assocData['page'] - 1) * $assocData['limit'];
} elseif (!isset($assocData['offset'])) {
$assocData['offset'] = null;
}
$limit = $this->limit($assocData['limit'], $assocData['offset']);
} }
$conditions .= $cond;
}
$conditions = $assocData['conditions']; $sql .= $this->conditions($conditions);
$sql = 'SELECT '; $sql .= $this->order($assocData['order']);
if ($this->goofyLimit) { if (!$this->goofyLimit) {
$sql .= $limit; $sql .= $limit;
}
$sql .= ' ' . join(', ', $this->fields($linkModel, $alias, $assocData['fields']));
$sql .= ' FROM ' . $this->fullTableName($linkModel) . ' ' . $this->alias . $this->name($alias);
if (is_array($conditions)) {
$conditions[$alias . '.' . $assocData['foreignKey']] = '{$__cakeID__$}';
} else {
$cond = $this->name($alias) . '.' . $this->name($assocData['foreignKey']);
$cond .= ' = {$__cakeID__$}';
if (trim($conditions) != '') {
$conditions .= ' AND ';
}
$conditions .= $cond;
}
$sql .= $this->conditions($conditions);
$sql .= $this->order($assocData['order']);
if (!$this->goofyLimit) {
$sql .= $limit;
}
} }
return $sql; return $sql;
break; break;
case 'hasAndBelongsToMany': case 'hasAndBelongsToMany':
if (isset($assocData['finderQuery']) && $assocData['finderQuery'] != null) { $joinTbl = $this->fullTableName($assocData['joinTable']);
$sql = $assocData['finderQuery']; $sql = 'SELECT ';
} else {
$joinTbl = $this->fullTableName($assocData['joinTable']);
$limit = ''; if ($this->goofyLimit) {
if (isset($assocData['limit'])) { $sql .= $limit;
if (!isset($assocData['offset']) && isset($assocData['page'])) { }
$assocData['offset'] = ($assocData['page'] - 1) * $assocData['limit']; $joinFields = array();
} elseif (!isset($assocData['offset'])) {
$assocData['offset'] = null; if (isset($assocData['with']) && is_array($assocData['with']) && !empty($assocData['with'])) {
} $joinName = array_keys($assocData['with']);
$limit = $this->limit($assocData['limit'], $assocData['offset']); $joinFields = $assocData['with'][$joinName[0]];
if (is_array($joinFields) && !empty($joinFields)) {
$joinFields = $this->fields($linkModel, $joinName[0], $joinFields);
} else {
$joinFields = array($this->name($joinName[0]) . '.*');
} }
}
$sql .= ' ' . join(', ', am($this->fields($linkModel, $alias, $assocData['fields']), $joinFields));
$sql .= ' FROM ' . $this->fullTableName($linkModel) . ' ' . $this->alias . $this->name($alias);
$sql .= ' JOIN ' . $joinTbl;
$sql = 'SELECT '; $joinAssoc = $joinTbl;
if ($this->goofyLimit) { if (isset($assocData['with']) && is_array($assocData['with']) && !empty($assocData['with'])) {
$sql .= $limit; $joinAssoc = $joinName[0];
} $sql .= $this->alias . $this->name($joinAssoc);
}
$sql .= ' ON ' . $this->name($joinAssoc);
$sql .= '.' . $this->name($assocData['foreignKey']) . ' = {$__cakeID__$}';
$sql .= ' AND ' . $this->name($joinAssoc) . '.' . $this->name($assocData['associationForeignKey']);
$sql .= ' = ' . $this->name($alias) . '.' . $this->name($linkModel->primaryKey);
$sql .= $this->conditions($assocData['conditions']);
$sql .= $this->order($assocData['order']);
$joinFields = array(); if (!$this->goofyLimit) {
if (isset($assocData['with']) && is_array($assocData['with']) && !empty($assocData['with'])) { $sql .= $limit;
$joinName = array_keys($assocData['with']);
$joinFields = $assocData['with'][$joinName[0]];
if (is_array($joinFields) && !empty($joinFields)) {
$joinFields = $this->fields($linkModel, $joinName[0], $joinFields);
} else {
$joinFields = array($this->name($joinName[0]) . '.*');
}
}
$sql .= ' ' . join(', ', am($this->fields($linkModel, $alias, $assocData['fields']), $joinFields));
$sql .= ' FROM ' . $this->fullTableName($linkModel) . ' ' . $this->alias . $this->name($alias);
$sql .= ' JOIN ' . $joinTbl;
$joinAssoc = $joinTbl;
if (isset($assocData['with']) && is_array($assocData['with']) && !empty($assocData['with'])) {
$joinAssoc = $joinName[0];
$sql .= $this->alias . $this->name($joinAssoc);
}
$sql .= ' ON ' . $this->name($joinAssoc);
$sql .= '.' . $this->name($assocData['foreignKey']) . ' = {$__cakeID__$}';
$sql .= ' AND ' . $this->name($joinAssoc) . '.' . $this->name($assocData['associationForeignKey']);
$sql .= ' = ' . $this->name($alias) . '.' . $this->name($linkModel->primaryKey);
$sql .= $this->conditions($assocData['conditions']);
$sql .= $this->order($assocData['order']);
if (!$this->goofyLimit) {
$sql .= $limit;
}
} }
return $sql; return $sql;
break; break;