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) {
@ -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,19 +997,6 @@ class DboSource extends DataSource {
break; break;
case 'hasMany': case 'hasMany':
if (isset($assocData['finderQuery']) && $assocData['finderQuery'] != null) {
$sql = $assocData['finderQuery'];
} else {
$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']);
}
$conditions = $assocData['conditions']; $conditions = $assocData['conditions'];
$sql = 'SELECT '; $sql = 'SELECT ';
@ -1076,32 +1025,17 @@ class DboSource extends DataSource {
if (!$this->goofyLimit) { if (!$this->goofyLimit) {
$sql .= $limit; $sql .= $limit;
} }
}
return $sql; return $sql;
break; break;
case 'hasAndBelongsToMany': case 'hasAndBelongsToMany':
if (isset($assocData['finderQuery']) && $assocData['finderQuery'] != null) {
$sql = $assocData['finderQuery'];
} else {
$joinTbl = $this->fullTableName($assocData['joinTable']); $joinTbl = $this->fullTableName($assocData['joinTable']);
$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']);
}
$sql = 'SELECT '; $sql = 'SELECT ';
if ($this->goofyLimit) { if ($this->goofyLimit) {
$sql .= $limit; $sql .= $limit;
} }
$joinFields = array(); $joinFields = array();
if (isset($assocData['with']) && is_array($assocData['with']) && !empty($assocData['with'])) { if (isset($assocData['with']) && is_array($assocData['with']) && !empty($assocData['with'])) {
$joinName = array_keys($assocData['with']); $joinName = array_keys($assocData['with']);
$joinFields = $assocData['with'][$joinName[0]]; $joinFields = $assocData['with'][$joinName[0]];
@ -1112,29 +1046,26 @@ class DboSource extends DataSource {
$joinFields = array($this->name($joinName[0]) . '.*'); $joinFields = array($this->name($joinName[0]) . '.*');
} }
} }
$sql .= ' ' . join(', ', am($this->fields($linkModel, $alias, $assocData['fields']), $joinFields)); $sql .= ' ' . join(', ', am($this->fields($linkModel, $alias, $assocData['fields']), $joinFields));
$sql .= ' FROM ' . $this->fullTableName($linkModel) . ' ' . $this->alias . $this->name($alias); $sql .= ' FROM ' . $this->fullTableName($linkModel) . ' ' . $this->alias . $this->name($alias);
$sql .= ' JOIN ' . $joinTbl; $sql .= ' JOIN ' . $joinTbl;
$joinAssoc = $joinTbl; $joinAssoc = $joinTbl;
if (isset($assocData['with']) && is_array($assocData['with']) && !empty($assocData['with'])) { if (isset($assocData['with']) && is_array($assocData['with']) && !empty($assocData['with'])) {
$joinAssoc = $joinName[0]; $joinAssoc = $joinName[0];
$sql .= $this->alias . $this->name($joinAssoc); $sql .= $this->alias . $this->name($joinAssoc);
} }
$sql .= ' ON ' . $this->name($joinAssoc); $sql .= ' ON ' . $this->name($joinAssoc);
$sql .= '.' . $this->name($assocData['foreignKey']) . ' = {$__cakeID__$}'; $sql .= '.' . $this->name($assocData['foreignKey']) . ' = {$__cakeID__$}';
$sql .= ' AND ' . $this->name($joinAssoc) . '.' . $this->name($assocData['associationForeignKey']); $sql .= ' AND ' . $this->name($joinAssoc) . '.' . $this->name($assocData['associationForeignKey']);
$sql .= ' = ' . $this->name($alias) . '.' . $this->name($linkModel->primaryKey); $sql .= ' = ' . $this->name($alias) . '.' . $this->name($linkModel->primaryKey);
$sql .= $this->conditions($assocData['conditions']); $sql .= $this->conditions($assocData['conditions']);
$sql .= $this->order($assocData['order']); $sql .= $this->order($assocData['order']);
if (!$this->goofyLimit) { if (!$this->goofyLimit) {
$sql .= $limit; $sql .= $limit;
} }
}
return $sql; return $sql;
break; break;
} }