Fixing issue in __saveMulti() where unique was not always honoured. Thanks 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
This commit is contained in:
mark_story 2009-01-16 04:09:44 +00:00
parent 9aabc8c893
commit 1111829e99
2 changed files with 34 additions and 9 deletions

View file

@ -1312,15 +1312,7 @@ class Model extends Overloadable {
} }
} }
if (!empty($newData)) { if ($this->hasAndBelongsToMany[$assoc]['unique']) {
foreach ($newData as $data) {
$data[$this->hasAndBelongsToMany[$assoc]['foreignKey']] = $id;
$this->{$join}->create($data);
$this->{$join}->save();
}
}
if (empty($newData) && $this->hasAndBelongsToMany[$assoc]['unique']) {
$associationForeignKey = "{$join}." . $this->hasAndBelongsToMany[$assoc]['associationForeignKey']; $associationForeignKey = "{$join}." . $this->hasAndBelongsToMany[$assoc]['associationForeignKey'];
$oldLinks = Set::extract($links, "{n}.{$associationForeignKey}"); $oldLinks = Set::extract($links, "{n}.{$associationForeignKey}");
if (!empty($oldLinks)) { if (!empty($oldLinks)) {
@ -1329,6 +1321,14 @@ class Model extends Overloadable {
} }
} }
if (!empty($newData)) {
foreach ($newData as $data) {
$data[$this->hasAndBelongsToMany[$assoc]['foreignKey']] = $id;
$this->{$join}->create($data);
$this->{$join}->save();
}
}
if (!empty($newValues)) { if (!empty($newValues)) {
$fields = join(',', $fields); $fields = join(',', $fields);
$db->insertMulti($this->{$join}, $fields, $newValues); $db->insertMulti($this->{$join}, $fields, $newValues);

View file

@ -2983,6 +2983,31 @@ class ModelTest extends CakeTestCase {
) )
); );
$this->assertEqual($result, $expected); $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 * testSaveHabtmCustomKeys method