From e7921c774174581857bcde6683bc3bcb05e7b372 Mon Sep 17 00:00:00 2001 From: phpnut Date: Wed, 29 Aug 2007 13:33:14 +0000 Subject: [PATCH] Initial work on single queries for hasAndBelongsToMany associations git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5607 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/model/datasources/dbo_source.php | 48 ++++++++++++++++++++-- 1 file changed, 44 insertions(+), 4 deletions(-) diff --git a/cake/libs/model/datasources/dbo_source.php b/cake/libs/model/datasources/dbo_source.php index b97c29bb5..16cf446e9 100644 --- a/cake/libs/model/datasources/dbo_source.php +++ b/cake/libs/model/datasources/dbo_source.php @@ -715,6 +715,23 @@ class DboSource extends DataSource { } return $this->__mergeHasMany($resultSet, $fetch, $association, $model, $linkModel, $recursive); } + + if ($type === 'hasAndBelongsToMany') { + $ins = $fetch = array(); + for ($i = 0; $i < $count; $i++) { + if ($in = $this->insertQueryData('{$__cakeID__$}', $resultSet[$i], $association, $assocData, $model, $linkModel, $stack)) { + $ins[] = $in; + } + } + if (!empty($ins)) { + $query = r('{$__cakeID__$}', '(' .join(', ', $ins) .')', $query); + $query = r('= (', 'IN (', $query); + $query = r(' WHERE 1 = 1', '', $query); + $fetch = $this->fetchAll($query, $model->cacheQueries, $model->name); + } + return $this->__mergeHabtm($resultSet, $fetch, $association, $model, $linkModel, $recursive); + } + for ($i = 0; $i < $count; $i++) { $row =& $resultSet[$i]; @@ -780,6 +797,30 @@ class DboSource extends DataSource { } } + function __mergeHabtm(&$resultSet, $merge, $association, &$model, &$linkModel) { + if (isset($model->hasAndBelongsToMany[$association])) { + $with = $model->hasAndBelongsToMany[$association]['with']; + $foreignKey = $model->hasAndBelongsToMany[$association]['foreignKey']; + $fields = $model->{$with}->loadInfo(); + $fields = $fields->extract('{n}.name'); + $count = count($fields); + + foreach ($resultSet as $i => $value) { + $merged = array(); + foreach ($merge as $j => $data) { + if($data[$with][$foreignKey] === $value[$model->name][$model->primaryKey]) { + if($count > 2) { + $merged[] = Set::pushDiff($merge[$j][$association], array($with => $data[$with])); + } else { + $merged[] = $merge[$j][$association]; + } + } + } + $resultSet[$i][$association] = $merged; + } + } + } + /** * Enter description here... * @@ -1082,16 +1123,15 @@ class DboSource extends DataSource { $joinAlias = $joinTbl; if (isset($assocData['with']) && !empty($assocData['with'])) { - $joinAssoc = $joinAlias = $model->{$assocData['with']}->name; $joinFields = $model->{$assocData['with']}->loadInfo(); $joinFields = $joinFields->extract('{n}.name'); - if (is_array($joinFields) && !empty($joinFields) && count($joinFields) > 2) { + if (is_array($joinFields) && !empty($joinFields)) { $joinFields = $this->fields($model->{$assocData['with']}, $model->{$assocData['with']}->name, $joinFields); + $joinAssoc = $joinAlias = $model->{$assocData['with']}->name; + } else { $joinFields = array(); - $joinAssoc = null; - $joinAlias = $joinTbl; } }