Permalink
Browse files

Fixing issue in __saveMulti() where unique was not always honoured. T…

…hanks for the patch 'Jafinto'. Tests added. Closes #6006

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@8001 3807eeeb-6ff5-0310-8944-8be069107fe0
  • Loading branch information...
1 parent 9aabc8c commit 1111829e9990786cc7a33afdbf292460555649e6 @markstory markstory committed Jan 16, 2009
Showing with 34 additions and 9 deletions.
  1. +9 −9 cake/libs/model/model.php
  2. +25 −0 cake/tests/cases/libs/model/model.test.php
View
@@ -1312,15 +1312,7 @@ function __saveMulti($joined, $id) {
}
}
- if (!empty($newData)) {
- foreach ($newData as $data) {
- $data[$this->hasAndBelongsToMany[$assoc]['foreignKey']] = $id;
- $this->{$join}->create($data);
- $this->{$join}->save();
- }
- }
-
- if (empty($newData) && $this->hasAndBelongsToMany[$assoc]['unique']) {
+ if ($this->hasAndBelongsToMany[$assoc]['unique']) {
$associationForeignKey = "{$join}." . $this->hasAndBelongsToMany[$assoc]['associationForeignKey'];
$oldLinks = Set::extract($links, "{n}.{$associationForeignKey}");
if (!empty($oldLinks)) {
@@ -1329,6 +1321,14 @@ function __saveMulti($joined, $id) {
}
}
+ if (!empty($newData)) {
+ foreach ($newData as $data) {
+ $data[$this->hasAndBelongsToMany[$assoc]['foreignKey']] = $id;
+ $this->{$join}->create($data);
+ $this->{$join}->save();
+ }
+ }
+
if (!empty($newValues)) {
$fields = join(',', $fields);
$db->insertMulti($this->{$join}, $fields, $newValues);
@@ -2983,6 +2983,31 @@ function testSaveHabtm() {
)
);
$this->assertEqual($result, $expected);
+
+
+ $this->loadFixtures('JoinA', 'JoinC', 'JoinAC', 'JoinB', 'JoinAB');
+ $TestModel = new JoinA();
+ $TestModel->hasBelongsToMany['JoinC']['unique'] = true;
+ $data = array(
+ 'JoinA' => array(
+ 'id' => 1,
+ 'name' => 'Join A 1',
+ 'body' => 'Join A 1 Body',
+ ),
+ 'JoinC' => array(
+ 'JoinC' => array(
+ array('join_c_id' => 2, 'other' => 'new record'),
+ array('join_c_id' => 3, 'other' => 'new record')
+ )
+ )
+ );
+ $TestModel->save($data);
+ $result = $TestModel->read(null, 1);
+ $time = date('Y-M-D H:i:s');
+ $expected = array(4, 5);
+ $this->assertEqual(Set::extract('/JoinC/JoinAsJoinC/id', $result), $expected);
+ $expected = array('new record', 'new record');
+ $this->assertEqual(Set::extract('/JoinC/JoinAsJoinC/other', $result), $expected);
}
/**
* testSaveHabtmCustomKeys method

0 comments on commit 1111829

Please sign in to comment.