Skip to content

Commit

Permalink
Fixed issue where Model::saveAll() would incorrectly commit a transac…
Browse files Browse the repository at this point in the history
…tion which was not started in that function call itself.
  • Loading branch information
ADmad committed Nov 6, 2010
1 parent 268dae7 commit eb76ab9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
20 changes: 14 additions & 6 deletions cake/libs/model/model.php
Expand Up @@ -559,9 +559,9 @@ function bindModel($params, $reset = true) {
*
* Example: Turn off the associated Model Support request,
* to temporarily lighten the User model:
*
*
* `$this->User->unbindModel( array('hasMany' => array('Supportrequest')) );`
*
*
* unbound models that are not made permanent will reset with the next call to Model::find()
*
* @param array $params Set of bindings to unbind (indexed by binding type)
Expand Down Expand Up @@ -1589,7 +1589,7 @@ function saveAll($data = null, $options = array()) {
}

if ($options['atomic'] && $options['validate'] !== 'only') {
$db->begin($this);
$transactionBegun = $db->begin($this);
}

if (Set::numeric(array_keys($data))) {
Expand Down Expand Up @@ -1629,8 +1629,12 @@ function saveAll($data = null, $options = array()) {
break;
default:
if ($options['atomic']) {
if ($validates && ($db->commit($this) !== false)) {
return true;
if ($validates) {
if ($transactionBegun) {
return $db->commit($this) !== false;
} else {
return true;
}
}
$db->rollback($this);
return false;
Expand Down Expand Up @@ -1740,7 +1744,11 @@ function saveAll($data = null, $options = array()) {
default:
if ($options['atomic']) {
if ($validates) {
return ($db->commit($this) !== false);
if ($transactionBegun) {
return $db->commit($this) !== false;
} else {
return true;
}
} else {
$db->rollback($this);
}
Expand Down
16 changes: 16 additions & 0 deletions cake/tests/cases/libs/model/model_write.test.php
Expand Up @@ -3121,6 +3121,22 @@ function testSaveAllAssociatedTransactionNoRollback() {
$Post->saveAll($data);
}

/**
* test saveAll with nested saveAll call.
*
* @return void
*/
function testSaveAllNestedSaveAll() {
$this->loadFixtures('Sample');
$TransactionTestModel =& new TransactionTestModel();

$data = array(
array('apple_id' => 1, 'name' => 'sample5'),
);

$this->assertTrue($TransactionTestModel->saveAll($data, array('atomic' => true)));
}

/**
* testSaveAllTransaction method
*
Expand Down
17 changes: 14 additions & 3 deletions cake/tests/cases/libs/model/models.php
Expand Up @@ -290,7 +290,7 @@ function titleDuplicate ($title) {
*/
class BeforeDeleteComment extends CakeTestModel {
var $name = 'BeforeDeleteComment';

var $useTable = 'comments';

function beforeDelete($cascade = true) {
Expand Down Expand Up @@ -3557,6 +3557,7 @@ class FruitNoWith extends CakeTestModel {
)
);
}

class UuidTagNoWith extends CakeTestModel {
var $name = 'UuidTag';
var $useTable = 'uuid_tags';
Expand All @@ -3573,11 +3574,21 @@ class UuidTagNoWith extends CakeTestModel {
class ProductUpdateAll extends CakeTestModel {
var $name = 'ProductUpdateAll';
var $useTable = 'product_update_all';

}

class GroupUpdateAll extends CakeTestModel {
var $name = 'GroupUpdateAll';
var $useTable = 'group_update_all';

}

class TransactionTestModel extends CakeTestModel {
var $name = 'TransactionTestModel';
var $useTable = 'samples';

function afterSave($created) {
$data = array(
array('apple_id' => 1, 'name' => 'sample6'),
);
$this->saveAll($data, array('atomic' => true, 'callbacks' => false));
}
}

0 comments on commit eb76ab9

Please sign in to comment.