Skip to content

Commit fd8ea89

Browse files
committed
Not aborting main saving operation if atomic is false and an association
fails saving
1 parent 7b3c569 commit fd8ea89

File tree

2 files changed

+71
-5
lines changed

2 files changed

+71
-5
lines changed

Cake/ORM/Table.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -953,8 +953,9 @@ protected function _processSave($entity, $options) {
953953

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

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

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

971972
if ($success) {
972-
$event = new Event('Model.afterSave', $this, compact('entity', 'options'));
973-
$this->getEventManager()->dispatch($event);
974-
$entity->isNew(false);
975973
$success = $this->_saveAssociations($children, $entity, $originalOptions);
974+
if ($success || !$options['atomic']) {
975+
$success = $entity;
976+
$event = new Event('Model.afterSave', $this, compact('entity', 'options'));
977+
$this->getEventManager()->dispatch($event);
978+
$entity->isNew(false);
979+
}
976980
}
977981

978982
if (!$success && $isNew) {

Cake/Test/TestCase/ORM/TableTest.php

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2047,7 +2047,7 @@ public function testSaveHasOneWithValidationError() {
20472047
$this->assertNull($entity->article->id);
20482048
$this->assertNull($entity->article->get('author_id'));
20492049
$this->assertTrue($entity->article->dirty('author_id'));
2050-
$this->assertNotEMpty($entity->article->errors('title'));
2050+
$this->assertNotEmpty($entity->article->errors('title'));
20512051
}
20522052

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

2165+
/**
2166+
* Tests saving hasOne association and returning a validation error will
2167+
* not abort the saving process if atomic is set to false
2168+
*
2169+
* @group save
2170+
* @return void
2171+
*/
2172+
public function testSaveHasOneWithValidationErrorNonAtomic() {
2173+
$entity = new \Cake\ORM\Entity([
2174+
'name' => 'Jose'
2175+
]);
2176+
$entity->article = new \Cake\ORM\Entity([
2177+
'title' => 'A Title',
2178+
'body' => 'A body'
2179+
]);
2180+
2181+
$table = TableRegistry::get('authors');
2182+
$table->hasOne('articles');
2183+
$table->association('articles')
2184+
->target()
2185+
->validator()
2186+
->add('title', 'num', ['rule' => 'numeric']);
2187+
2188+
$this->assertSame($entity, $table->save($entity, ['atomic' => false]));
2189+
$this->assertFalse($entity->isNew());
2190+
$this->assertTrue($entity->article->isNew());
2191+
$this->assertNull($entity->article->id);
2192+
$this->assertNull($entity->article->get('author_id'));
2193+
$this->assertTrue($entity->article->dirty('author_id'));
2194+
$this->assertNotEmpty($entity->article->errors('title'));
2195+
}
2196+
2197+
/**
2198+
* Tests saving belongsTo association and get a validation error won't stop
2199+
* saving if atomic is set to false
2200+
*
2201+
* @group save
2202+
* @return void
2203+
*/
2204+
public function testsSaveBelongsToWithValidationErrorNotAtomic() {
2205+
$entity = new \Cake\ORM\Entity([
2206+
'title' => 'A Title',
2207+
'body' => 'A body'
2208+
]);
2209+
$entity->author = new \Cake\ORM\Entity([
2210+
'name' => 'Jose'
2211+
]);
2212+
2213+
$table = TableRegistry::get('articles');
2214+
$table->belongsTo('authors');
2215+
$table->association('authors')
2216+
->target()
2217+
->validator()
2218+
->add('name', 'num', ['rule' => 'numeric']);
2219+
2220+
$this->assertSame($entity, $table->save($entity));
2221+
$this->assertFalse($entity->isNew());
2222+
$this->assertTrue($entity->author->isNew());
2223+
$this->assertNull($entity->get('author_id'));
2224+
$this->assertNotEmpty($entity->author->errors('name'));
2225+
}
2226+
21652227
}

0 commit comments

Comments
 (0)