Refactoring association joins, Fixes the issue with adding the Model.id if it is not already added.

Adding fix for string based fields.


git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@4831 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
phpnut 2007-04-09 21:30:55 +00:00
parent d416fbe4ed
commit 8ed1b69b3f
2 changed files with 58 additions and 23 deletions

View file

@ -724,7 +724,10 @@ class DboSource extends DataSource {
if($type === 'hasMany') { if($type === 'hasMany') {
$ins = array(); $ins = array();
for($i = 0; $i < $count; $i++) { for($i = 0; $i < $count; $i++) {
$ins[] = $this->insertQueryData('{$__cakeID__$}', $resultSet[$i], $association, $assocData, $model, $linkModel, $stack); $in = $this->insertQueryData('{$__cakeID__$}', $resultSet[$i], $association, $assocData, $model, $linkModel, $stack);
if ($in !== false) {
$ins[] = $in;
}
} }
if(!empty($ins)){ if(!empty($ins)){
@ -990,6 +993,22 @@ class DboSource extends DataSource {
if (empty($queryData['fields'])) { if (empty($queryData['fields'])) {
$queryData['fields'] = $this->fields($model, $model->name); $queryData['fields'] = $this->fields($model, $model->name);
} elseif(!empty($model->hasMany)) {
$assocFields = $this->fields($model, $model->name, array("{$model->name}.{$model->primaryKey}"));
$passedFields = $this->fields($model, $model->name, $queryData['fields']);
if(count($passedFields) === 1) {
$match = strrpos($passedFields[0], $assocFields[0]);
$match1 = strrpos($passedFields[0], 'COUNT(');
if($match === false && $match1 === false){
$queryData['fields'] = array_unique(array_merge($passedFields, $assocFields));
} else {
$queryData['fields'] = $passedFields;
}
} else {
$queryData['fields'] = array_unique(array_merge($passedFields, $assocFields));
}
unset($assocFields);
unset($passedFields);
} }
if ($linkModel == null) { if ($linkModel == null) {
@ -1078,9 +1097,14 @@ class DboSource extends DataSource {
} }
break; break;
case 'hasMany': case 'hasMany':
$assocFields = $this->fields($linkModel, $alias, array("{$alias}.{$assocData['foreignKey']}"));
$passedFields = $this->fields($linkModel, $alias, $assocData['fields']);
$assocData['fields'] = array_unique(array_merge($passedFields, $assocFields));
unset($assocFields);
unset($passedFields);
$query = array( $query = array(
'conditions' => $this->__mergeConditions(array("{$alias}.{$assocData['foreignKey']}" => array('{$__cakeID__$}')), $assocData['conditions']), 'conditions' => $this->__mergeConditions(array("{$alias}.{$assocData['foreignKey']}" => array('{$__cakeID__$}')), $assocData['conditions']),
'fields' => $this->fields($linkModel, $alias, $assocData['fields']), 'fields' => $assocData['fields'],
'table' => $this->fullTableName($linkModel), 'table' => $this->fullTableName($linkModel),
'alias' => $alias, 'alias' => $alias,
'order' => $assocData['order'], 'order' => $assocData['order'],
@ -1381,7 +1405,6 @@ class DboSource extends DataSource {
} }
} }
} }
if (empty($fields)) { if (empty($fields)) {
$fieldData = $model->loadInfo(); $fieldData = $model->loadInfo();
$fields = $fieldData->extract('{n}.name'); $fields = $fieldData->extract('{n}.name');
@ -1407,10 +1430,22 @@ class DboSource extends DataSource {
if ($dot === false) { if ($dot === false) {
$fields[$i] = $prepend . $this->name($alias) . '.' . $this->name($fields[$i]); $fields[$i] = $prepend . $this->name($alias) . '.' . $this->name($fields[$i]);
} else { } else {
$comma = strrpos($fields[$i], ',');
if ($comma === false) {
$build = explode('.', $fields[$i]); $build = explode('.', $fields[$i]);
if (!Set::numeric($build)) { if (!Set::numeric($build)) {
$fields[$i] = $prepend . $this->name($build[0]) . '.' . $this->name($build[1]); $fields[$i] = $prepend . $this->name($build[0]) . '.' . $this->name($build[1]);
} }
} else {
$comma = explode(',', $fields[$i]);
foreach ($comma as $string) {
$build = explode('.', $string);
if (!Set::numeric($build)) {
$value[] = $prepend . $this->name(trim($build[0])) . '.' . $this->name(trim($build[1]));
}
}
$fields[$i] = implode(', ', $value);
}
} }
} elseif (preg_match('/\(([\.\w]+)\)/', $fields[$i], $field)) { } elseif (preg_match('/\(([\.\w]+)\)/', $fields[$i], $field)) {
if (isset($field[1])) { if (isset($field[1])) {

View file

@ -972,7 +972,7 @@ class DboSourceTest extends UnitTestCase {
$this->assertPattern('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+IN\s+\({\$__cakeID__\$}\)(?:\))?/', $result); $this->assertPattern('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+IN\s+\({\$__cakeID__\$}\)(?:\))?/', $result);
$result = $this->db->generateAssociationQuery($this->model, $null, null, null, null, $queryData, false, $null); $result = $this->db->generateAssociationQuery($this->model, $null, null, null, null, $queryData, false, $null);
$this->assertPattern('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`name`\s+/', $result); $this->assertPattern('/^SELECT\s+`TestModel5`\.`name`, `TestModel5`\.`id`\s+/', $result);
$this->assertPattern('/\s+FROM\s+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result); $this->assertPattern('/\s+FROM\s+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result);
$this->assertPattern('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result); $this->assertPattern('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result);
@ -1006,7 +1006,7 @@ class DboSourceTest extends UnitTestCase {
$this->assertPattern('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+IN\s+\({\$__cakeID__\$}\)(?:\))?/', $result); $this->assertPattern('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+IN\s+\({\$__cakeID__\$}\)(?:\))?/', $result);
$result = $this->db->generateAssociationQuery($this->model, $null, null, null, null, $queryData, false, $null); $result = $this->db->generateAssociationQuery($this->model, $null, null, null, null, $queryData, false, $null);
$this->assertPattern('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`name`, `TestModel5`\.`created`\s+/', $result); $this->assertPattern('/^SELECT\s+`TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`id`\s+/', $result);
$this->assertPattern('/\s+FROM\s+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result); $this->assertPattern('/\s+FROM\s+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result);
$this->assertPattern('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result); $this->assertPattern('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result);
@ -1020,7 +1020,7 @@ class DboSourceTest extends UnitTestCase {
$params = &$this->_prepareAssociationQuery($this->model, $queryData, $binding); $params = &$this->_prepareAssociationQuery($this->model, $queryData, $binding);
$result = $this->db->generateAssociationQuery($this->model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet); $result = $this->db->generateAssociationQuery($this->model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
$this->assertPattern('/^SELECT\s+`TestModel6`\.`test_model5_id`, `TestModel6`\.`name`\s+/', $result); $this->assertPattern('/^SELECT\s+`TestModel6`\.`name`, `TestModel6`\.`test_model5_id`\s+/', $result);
$this->assertPattern('/\s+FROM\s+`test_model6` AS `TestModel6`\s+WHERE\s+/', $result); $this->assertPattern('/\s+FROM\s+`test_model6` AS `TestModel6`\s+WHERE\s+/', $result);
$this->assertPattern('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+IN\s+\({\$__cakeID__\$}\)(?:\))?/', $result); $this->assertPattern('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+IN\s+\({\$__cakeID__\$}\)(?:\))?/', $result);
@ -1041,7 +1041,7 @@ class DboSourceTest extends UnitTestCase {
$params = &$this->_prepareAssociationQuery($this->model, $queryData, $binding); $params = &$this->_prepareAssociationQuery($this->model, $queryData, $binding);
$result = $this->db->generateAssociationQuery($this->model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet); $result = $this->db->generateAssociationQuery($this->model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
$this->assertPattern('/^SELECT\s+`TestModel6`\.`id`, `TestModel6`\.`test_model5_id`, `TestModel6`\.`name`\s+/', $result); $this->assertPattern('/^SELECT\s+`TestModel6`\.`id`, `TestModel6`\.`name`, `TestModel6`\.`test_model5_id`\s+/', $result);
$this->assertPattern('/\s+FROM\s+`test_model6` AS `TestModel6`\s+WHERE\s+/', $result); $this->assertPattern('/\s+FROM\s+`test_model6` AS `TestModel6`\s+WHERE\s+/', $result);
$this->assertPattern('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+IN\s+\({\$__cakeID__\$}\)(?:\))?/', $result); $this->assertPattern('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+IN\s+\({\$__cakeID__\$}\)(?:\))?/', $result);