Skip to content

Commit

Permalink
Fixing issue in __saveMulti() where unique was not always honoured. T…
Browse files Browse the repository at this point in the history
…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
markstory committed Jan 16, 2009
1 parent 9aabc8c commit 1111829
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
18 changes: 9 additions & 9 deletions cake/libs/model/model.php
Expand Up @@ -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)) {
Expand All @@ -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);
Expand Down
25 changes: 25 additions & 0 deletions cake/tests/cases/libs/model/model.test.php
Expand Up @@ -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
Expand Down

0 comments on commit 1111829

Please sign in to comment.