Fixing HABTM saving with saveAll(). Tests added. Fixes #4389

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7789 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
mark_story 2008-10-29 00:22:52 +00:00
parent b5a77bc0a9
commit ee8ff1d5ac
2 changed files with 35 additions and 2 deletions

View file

@ -1402,6 +1402,7 @@ class Model extends Overloadable {
case 'belongsTo':
if ($this->__save($this->{$association}, $values, $options)) {
$data[$this->alias][$this->belongsTo[$association]['foreignKey']] = $this->{$association}->id;
unset($data[$association]);
} else {
$validationErrors[$association] = $this->{$association}->validationErrors;
$validates = false;
@ -1413,7 +1414,7 @@ class Model extends Overloadable {
}
}
}
if (!$this->__save($this, $data[$this->alias], $options)) {
if (!$this->__save($this, $data, $options)) {
$validationErrors[$this->alias] = $this->validationErrors;
$validates = false;
}

View file

@ -2996,6 +2996,38 @@ class ModelTest extends CakeTestCase {
$expected = array('id' => '2', 'comment_id' => '7', 'attachment' => 'some_file.tgz', 'created' => $ts, 'updated' => $ts);
$this->assertEqual($result[6]['Attachment'], $expected);
}
/**
* Test SaveAll with Habtm relations
*
* @return void
**/
function testSaveAllHabtm() {
$this->loadFixtures('Article', 'Tag', 'Comment', 'User');
$data = array(
'Article' => array(
'user_id' => 1, 'title' => 'RRticle Has and belongs to Many Tags'
),
'Tag' => array(
'Tag' => array(
1, 2
)
),
'Comment' => array(
array('comment' => 'Article comment', 'user_id' => 1),
),
);
$Article =& new Article();
$result = $Article->saveAll($data);
$this->assertTrue($result);
$result = $Article->read();
$this->assertEqual(count($result['Tag']), 2);
$this->assertEqual($result['Tag'][0]['tag'], 'tag1');
$this->assertEqual(count($result['Comment']), 1);
$this->assertEqual(count($result['Comment'][0]['comment']['Article comment']), 1);
}
/**
* testSaveAllHasOne method
*
@ -3186,7 +3218,7 @@ class ModelTest extends CakeTestCase {
$expected = array('First Comment for Second Article', 'Second Comment for Second Article', 'First new comment', 'Second new comment', 'Third new comment');
$this->assertEqual(Set::extract($result['Comment'], '{n}.comment'), $expected);
}
/**
/**
* testSaveAllHasManyValidation method
*
* @access public