mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 09:06:17 +00:00
Initial work on using one select statement for hasMany associations
git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@4824 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
parent
9a6d54b400
commit
95e2782d72
2 changed files with 41 additions and 13 deletions
|
@ -1018,7 +1018,7 @@ class DboSource extends DataSource {
|
|||
break;
|
||||
case 'hasMany':
|
||||
$query = array(
|
||||
'conditions' => $this->__mergeConditions(array("{$alias}.{$assocData['foreignKey']}" => '{$__cakeID__$}'), $assocData['conditions']),
|
||||
'in' => $this->__mergeConditions(array("{$alias}.{$assocData['foreignKey']}" => array('{$__cakeID__$}')), $assocData['conditions']),
|
||||
'fields' => $this->fields($linkModel, $alias, $assocData['fields']),
|
||||
'table' => $this->fullTableName($linkModel),
|
||||
'alias' => $alias,
|
||||
|
@ -1084,6 +1084,9 @@ class DboSource extends DataSource {
|
|||
}
|
||||
|
||||
function buildStatement($query, $model) {
|
||||
if(isset($query['in'])) {
|
||||
return $this->renderInStatement($query, $model);
|
||||
}
|
||||
$query = am(array('offset' => null, 'joins' => array()), $query);
|
||||
if (!empty($query['joins'])) {
|
||||
for ($i = 0; $i < count($query['joins']); $i++) {
|
||||
|
@ -1103,6 +1106,31 @@ class DboSource extends DataSource {
|
|||
));
|
||||
}
|
||||
|
||||
function renderInStatement($query, $model) {
|
||||
if(isset($query['in'])) {
|
||||
$query['conditions'] = $query['in'];
|
||||
}
|
||||
$query = am(array('offset' => null, 'joins' => array()), $query);
|
||||
if (!empty($query['joins'])) {
|
||||
for ($i = 0; $i < count($query['joins']); $i++) {
|
||||
if (is_array($query['joins'][$i])) {
|
||||
$query['joins'][$i] = $this->buildJoinStatement($query['joins'][$i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
$replace[] = '{$__cakeID__$}';
|
||||
$replace[] = $this->renderStatement(array(
|
||||
'conditions' => $this->conditions($query['conditions']),
|
||||
'fields' => join(', ', $query['fields']),
|
||||
'table' => $query['table'],
|
||||
'alias' => $this->alias . $this->name($query['alias']),
|
||||
'order' => $this->order($query['order']),
|
||||
'limit' => $this->limit($query['limit'], $query['offset']),
|
||||
'joins' => join(' ', $query['joins'])
|
||||
));
|
||||
return $replace[1];
|
||||
}
|
||||
|
||||
function renderJoinStatement($data) {
|
||||
extract($data);
|
||||
return trim("{$type} JOIN {$table} {$alias} ON ({$conditions})");
|
||||
|
|
|
@ -923,7 +923,7 @@ class DboSourceTest extends UnitTestCase {
|
|||
$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`, `TestModel6`\.`created`, `TestModel6`\.`updated`\s+/', $result);
|
||||
$this->assertPattern('/\s+FROM\s+`test_model6` AS `TestModel6`\s+WHERE/', $result);
|
||||
$this->assertPattern('/\s+WHERE\s+`TestModel6`.`test_model5_id`\s+=\s+{\$__cakeID__\$}/', $result);
|
||||
$this->assertPattern('/\s+WHERE\s+`TestModel6`.`test_model5_id`\s+IN\s+\({\$__cakeID__\$}\)/', $result);
|
||||
|
||||
$result = $this->db->generateAssociationQuery($this->model, $null, null, null, null, $queryData, false, $null);
|
||||
$this->assertPattern('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`\s+/', $result);
|
||||
|
@ -946,7 +946,7 @@ class DboSourceTest extends UnitTestCase {
|
|||
$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`, `TestModel6`\.`created`, `TestModel6`\.`updated`\s+/', $result);
|
||||
$this->assertPattern('/\s+FROM\s+`test_model6` AS `TestModel6`\s+WHERE\s+/', $result);
|
||||
$this->assertPattern('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\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);
|
||||
$this->assertPattern('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`\s+/', $result);
|
||||
|
|
Loading…
Add table
Reference in a new issue