|
|
@@ -1715,7 +1715,7 @@ class Model extends Object {
|
|
|
)
|
|
|
);
|
|
|
|
|
|
- $newData = $newValues = array();
|
|
|
+ $newData = $newValues = $newJoins = array();
|
|
|
$primaryAdded = false;
|
|
|
|
|
|
$fields = array(
|
|
|
@@ -1731,11 +1731,12 @@ class Model extends Object {
|
|
|
|
|
|
foreach ((array)$data as $row) {
|
|
|
if ((is_string($row) && (strlen($row) == 36 || strlen($row) == 16)) || is_numeric($row)) {
|
|
|
+ $newJoins[] = $row;
|
|
|
$values = array($id, $row);
|
|
|
if ($isUUID && $primaryAdded) {
|
|
|
$values[] = String::uuid();
|
|
|
}
|
|
|
- $newValues[] = $values;
|
|
|
+ $newValues[$row] = $values;
|
|
|
unset($values);
|
|
|
} elseif (isset($row[$this->hasAndBelongsToMany[$assoc]['associationForeignKey']])) {
|
|
|
$newData[] = $row;
|
|
|
@@ -1744,6 +1745,7 @@ class Model extends Object {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ $keepExisting = $this->hasAndBelongsToMany[$assoc]['unique'] === 'keepExisting';
|
|
|
if ($this->hasAndBelongsToMany[$assoc]['unique']) {
|
|
|
$conditions = array(
|
|
|
$join . '.' . $this->hasAndBelongsToMany[$assoc]['foreignKey'] => $id
|
|
|
@@ -1751,16 +1753,20 @@ class Model extends Object {
|
|
|
if (!empty($this->hasAndBelongsToMany[$assoc]['conditions'])) {
|
|
|
$conditions = array_merge($conditions, (array)$this->hasAndBelongsToMany[$assoc]['conditions']);
|
|
|
}
|
|
|
+ $associationForeignKey = $this->{$join}->alias .'.'. $this->hasAndBelongsToMany[$assoc]['associationForeignKey'];
|
|
|
$links = $this->{$join}->find('all', array(
|
|
|
'conditions' => $conditions,
|
|
|
'recursive' => empty($this->hasAndBelongsToMany[$assoc]['conditions']) ? -1 : 0,
|
|
|
- 'fields' => $this->hasAndBelongsToMany[$assoc]['associationForeignKey']
|
|
|
+ 'fields' => $associationForeignKey,
|
|
|
));
|
|
|
|
|
|
- $associationForeignKey = "{$join}." . $this->hasAndBelongsToMany[$assoc]['associationForeignKey'];
|
|
|
$oldLinks = Set::extract($links, "{n}.{$associationForeignKey}");
|
|
|
if (!empty($oldLinks)) {
|
|
|
- $conditions[$associationForeignKey] = $oldLinks;
|
|
|
+ if ($keepExisting && !empty($newJoins)) {
|
|
|
+ $conditions[$associationForeignKey] = array_diff($oldLinks, $newJoins);
|
|
|
+ } else {
|
|
|
+ $conditions[$associationForeignKey] = $oldLinks;
|
|
|
+ }
|
|
|
$dbMulti->delete($this->{$join}, $conditions);
|
|
|
}
|
|
|
}
|
|
|
@@ -1774,7 +1780,21 @@ class Model extends Object {
|
|
|
}
|
|
|
|
|
|
if (!empty($newValues)) {
|
|
|
- $dbMulti->insertMulti($this->{$join}, $fields, $newValues);
|
|
|
+ if ($keepExisting && !empty($links)) {
|
|
|
+ foreach ($links as $link) {
|
|
|
+ $oldJoin = $link[$join][$this->hasAndBelongsToMany[$assoc]['associationForeignKey']];
|
|
|
+ if (! in_array($oldJoin, $newJoins) ) {
|
|
|
+ $conditions[$associationForeignKey] = $oldJoin;
|
|
|
+ $db->delete($this->{$join}, $conditions);
|
|
|
+ } else {
|
|
|
+ unset($newValues[$oldJoin]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $newValues = array_values($newValues);
|
|
|
+ }
|
|
|
+ if (!empty($newValues)) {
|
|
|
+ $dbMulti->insertMulti($this->{$join}, $fields, $newValues);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|