mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-19 02:56:15 +00:00
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
This commit is contained in:
parent
d20ddfeadc
commit
e7921c7741
1 changed files with 44 additions and 4 deletions
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue