Skip to content

Commit

Permalink
Adding a missing rollback when validation on an associated record fai…
Browse files Browse the repository at this point in the history
…ls, and validate = first. Fixes #1147
  • Loading branch information
markstory committed Sep 29, 2010
1 parent 0761ede commit b08aba8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
6 changes: 5 additions & 1 deletion cake/libs/model/model.php
Expand Up @@ -1665,6 +1665,7 @@ function saveAll($data = null, $options = array()) {
}
}
}

if (!$this->__save($data, $options)) {
$validationErrors[$this->alias] = $this->validationErrors;
$validates = false;
Expand Down Expand Up @@ -1735,7 +1736,6 @@ function saveAll($data = null, $options = array()) {
case ($options['validate'] === 'first'):
$options['validate'] = true;
$return = array();
continue;
break;
default:
if ($options['atomic']) {
Expand All @@ -1748,6 +1748,10 @@ function saveAll($data = null, $options = array()) {
return $return;
break;
}
if ($options['atomic'] && !$validates) {
$db->rollback($this);
return false;
}
}
}

Expand Down
40 changes: 39 additions & 1 deletion cake/tests/cases/libs/model/model_write.test.php
Expand Up @@ -3039,7 +3039,7 @@ function testSaveAllHasManyValidation() {
*
* @return void
*/
function testSaveAllTransactionNoRollback() {
function testSaveAllManyRowsTransactionNoRollback() {
$this->loadFixtures('Post');

Mock::generate('DboSource', 'MockTransactionDboSource');
Expand All @@ -3062,6 +3062,44 @@ function testSaveAllTransactionNoRollback() {
$Post->saveAll($data, array('atomic' => true));
}

/**
* test saveAll with transactions and ensure there is no missing rollback.
*
* @return void
*/
function testSaveAllAssociatedTransactionNoRollback() {
$testDb = ConnectionManager::getDataSource('test_suite');

Mock::generate('DboSource', 'MockTransactionAssociatedDboSource');
$db = ConnectionManager::create('mock_transaction_assoc', array(
'datasource' => 'MockTransactionAssociatedDbo',
));
$db->columns = $testDb->columns;

$db->expectOnce('rollback');

$Post =& new Post();
$Post->useDbConfig = 'mock_transaction_assoc';
$Post->Author->useDbConfig = 'mock_transaction_assoc';

$Post->Author->validate = array(
'user' => array('rule' => array('notEmpty'))
);

$data = array(
'Post' => array(
'title' => 'New post',
'body' => 'Content',
'published' => 'Y'
),
'Author' => array(
'user' => '',
'password' => "sekret"
)
);
$Post->saveAll($data);
}

/**
* testSaveAllTransaction method
*
Expand Down

0 comments on commit b08aba8

Please sign in to comment.