Skip to content

Commit

Permalink
Not aborting main saving operation if atomic is false and an association
Browse files Browse the repository at this point in the history
fails saving
  • Loading branch information
lorenzo committed Nov 25, 2013
1 parent 7b3c569 commit fd8ea89
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 5 deletions.
12 changes: 8 additions & 4 deletions Cake/ORM/Table.php
Expand Up @@ -953,8 +953,9 @@ protected function _processSave($entity, $options) {

$originalOptions = $options->getArrayCopy();
list($parents, $children) = $this->_sortAssociationTypes($options['associated']);
$saved = $this->_saveAssociations($parents, $entity, $originalOptions);

if (!$this->_saveAssociations($parents, $entity, $originalOptions)) {
if (!$saved && $options['atomic']) {
return false;
}

Expand All @@ -969,10 +970,13 @@ protected function _processSave($entity, $options) {
}

if ($success) {
$event = new Event('Model.afterSave', $this, compact('entity', 'options'));
$this->getEventManager()->dispatch($event);
$entity->isNew(false);
$success = $this->_saveAssociations($children, $entity, $originalOptions);
if ($success || !$options['atomic']) {
$success = $entity;
$event = new Event('Model.afterSave', $this, compact('entity', 'options'));
$this->getEventManager()->dispatch($event);
$entity->isNew(false);
}
}

if (!$success && $isNew) {
Expand Down
64 changes: 63 additions & 1 deletion Cake/Test/TestCase/ORM/TableTest.php
Expand Up @@ -2047,7 +2047,7 @@ public function testSaveHasOneWithValidationError() {
$this->assertNull($entity->article->id);
$this->assertNull($entity->article->get('author_id'));
$this->assertTrue($entity->article->dirty('author_id'));
$this->assertNotEMpty($entity->article->errors('title'));
$this->assertNotEmpty($entity->article->errors('title'));
}

/**
Expand Down Expand Up @@ -2162,4 +2162,66 @@ public function testSaveHasManyWithErrorsNonAtomic() {
$this->assertEquals(5, $entity->articles[1]->author_id);
}

/**
* Tests saving hasOne association and returning a validation error will
* not abort the saving process if atomic is set to false
*
* @group save
* @return void
*/
public function testSaveHasOneWithValidationErrorNonAtomic() {
$entity = new \Cake\ORM\Entity([
'name' => 'Jose'
]);
$entity->article = new \Cake\ORM\Entity([
'title' => 'A Title',
'body' => 'A body'
]);

$table = TableRegistry::get('authors');
$table->hasOne('articles');
$table->association('articles')
->target()
->validator()
->add('title', 'num', ['rule' => 'numeric']);

$this->assertSame($entity, $table->save($entity, ['atomic' => false]));
$this->assertFalse($entity->isNew());
$this->assertTrue($entity->article->isNew());
$this->assertNull($entity->article->id);
$this->assertNull($entity->article->get('author_id'));
$this->assertTrue($entity->article->dirty('author_id'));
$this->assertNotEmpty($entity->article->errors('title'));
}

/**
* Tests saving belongsTo association and get a validation error won't stop
* saving if atomic is set to false
*
* @group save
* @return void
*/
public function testsSaveBelongsToWithValidationErrorNotAtomic() {
$entity = new \Cake\ORM\Entity([
'title' => 'A Title',
'body' => 'A body'
]);
$entity->author = new \Cake\ORM\Entity([
'name' => 'Jose'
]);

$table = TableRegistry::get('articles');
$table->belongsTo('authors');
$table->association('authors')
->target()
->validator()
->add('name', 'num', ['rule' => 'numeric']);

$this->assertSame($entity, $table->save($entity));
$this->assertFalse($entity->isNew());
$this->assertTrue($entity->author->isNew());
$this->assertNull($entity->get('author_id'));
$this->assertNotEmpty($entity->author->errors('name'));
}

}

0 comments on commit fd8ea89

Please sign in to comment.