Adding fix for Model::saveAll() with 'validate' => 'first' and hasMany records, fixes #4387, adding test cases to disprove #4494

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6897 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
nate 2008-05-16 05:31:14 +00:00
parent 5893fd3669
commit 0f1e075d68
2 changed files with 88 additions and 15 deletions

View file

@ -1388,14 +1388,29 @@ class Model extends Overloadable {
foreach ($values as $i => $value) { foreach ($values as $i => $value) {
$values[$i][$this->{$type}[$association]['foreignKey']] = $this->id; $values[$i][$this->{$type}[$association]['foreignKey']] = $this->id;
} }
$_return = $this->{$association}->saveAll($values, array_merge($options, array('atomic' => false))); $_options = array_merge($options, array('atomic' => false));
if (in_array(false, $_return)) {
if ($_options['validate'] === 'first') {
$_options['validate'] = 'only';
}
$_return = $this->{$association}->saveAll($values, $_options);
if ($_return === false || (is_array($_return) && in_array(false, $_return, true))) {
$validationErrors[$association] = $this->{$association}->validationErrors; $validationErrors[$association] = $this->{$association}->validationErrors;
$validates = false; $validates = false;
} }
if (is_array($_return)) {
foreach ($_return as $val) { foreach ($_return as $val) {
if (!isset($return[$association])) {
$return[$association] = array();
} elseif (!is_array($return[$association])) {
$return[$association] = array($return[$association]);
}
$return[$association][] = $val; $return[$association][] = $val;
} }
} else {
$return[$association] = $_return;
}
break; break;
} }
} }

View file

@ -2138,6 +2138,50 @@ class ModelTest extends CakeTestCase {
$this->assertEqual($result[6]['Attachment'], $expected); $this->assertEqual($result[6]['Attachment'], $expected);
} }
function testSaveAllHasOne() {
$model = new Comment();
$model->deleteAll(true);
$this->assertEqual($model->find('all'), array());
$model->Attachment->deleteAll(true);
$this->assertEqual($model->Attachment->find('all'), array());
$this->assertTrue($model->saveAll(array(
'Comment' => array('comment' => 'Comment with attachment', 'article_id' => 1, 'user_id' => 1),
'Attachment' => array('attachment' => 'some_file.zip')
)));
$result = $model->find('all', array('fields' => array(
'Comment.id', 'Comment.comment', 'Attachment.id', 'Attachment.comment_id', 'Attachment.attachment'
)));
$expected = array(array(
'Comment' => array('id' => '1', 'comment' => 'Comment with attachment'),
'Attachment' => array('id' => '1', 'comment_id' => '1', 'attachment' => 'some_file.zip')
));
$this->assertEqual($result, $expected);
}
function testSaveAllBelongsTo() {
$model = new Comment();
$model->deleteAll(true);
$this->assertEqual($model->find('all'), array());
$model->Article->deleteAll(true);
$this->assertEqual($model->Article->find('all'), array());
$this->assertTrue($model->saveAll(array(
'Comment' => array('comment' => 'Article comment', 'article_id' => 1, 'user_id' => 1),
'Article' => array('title' => 'Model Associations 101', 'user_id' => 1)
)));
$result = $model->find('all', array('fields' => array(
'Comment.id', 'Comment.comment', 'Comment.article_id', 'Article.id', 'Article.title'
)));
$expected = array(array(
'Comment' => array('id' => '1', 'article_id' => '1', 'comment' => 'Article comment'),
'Article' => array('id' => '1', 'title' => 'Model Associations 101')
));
$this->assertEqual($result, $expected);
}
function testSaveAllAtomic() { function testSaveAllAtomic() {
$this->loadFixtures('Article', 'User'); $this->loadFixtures('Article', 'User');
$TestModel =& new Article(); $TestModel =& new Article();
@ -2385,24 +2429,38 @@ class ModelTest extends CakeTestCase {
} }
function testSaveAllValidateFirst() { function testSaveAllValidateFirst() {
$TestModel =& new Article(); $model =& new Article();
$TestModel->deleteAll(true); $model->deleteAll(true);
$TestModel->Comment->validate = array('comment' => VALID_NOT_EMPTY); $model->Comment->validate = array('comment' => VALID_NOT_EMPTY);
$result = $TestModel->saveAll(array( $result = $model->saveAll(array(
'Article' => array('title' => 'Post with Author', 'body' => 'This post will be saved author'), 'Article' => array('title' => 'Post with Author', 'body' => 'This post will be saved author'),
'Comment' => array( 'Comment' => array(
array('comment' => 'First new comment'), array('comment' => 'First new comment'),
array('comment' => '') array('comment' => '')
) )
), array('validate' => 'first')); ), array('validate' => 'first'));
$this->assertFalse($result); $this->assertFalse($result);
$result = $TestModel->find('all'); $result = $model->find('all');
$this->assertEqual($result, array()); $this->assertEqual($result, array());
$expected = array('Comment' => array(0 => array('comment' => 'This field cannot be left blank'))); $expected = array('Comment' => array(0 => array('comment' => 'This field cannot be left blank')));
$this->assertEqual($TestModel->validationErrors, $expected); $this->assertEqual($model->validationErrors, $expected);
$this->assertIdentical($model->Comment->find('count'), 0);
$result = $model->saveAll(array(
'Article' => array('title' => 'Post with Author', 'body' => 'This post will be saved without an author'),
'Comment' => array(
array('comment' => 'Only new comment'),
)
), array('validate' => 'first'));
$this->assertIdentical($result, true);
$result = $model->Comment->find('all');
$this->assertIdentical(count($result), 1);
$result = Set::extract('/Comment/article_id', $result);
$this->assertTrue($result[0] === 1 || $result[0] === '1');
} }
function testSaveWithCounterCache() { function testSaveWithCounterCache() {