diff --git a/Cake/ORM/Table.php b/Cake/ORM/Table.php index b9357d694a8..2007d18587d 100644 --- a/Cake/ORM/Table.php +++ b/Cake/ORM/Table.php @@ -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; } @@ -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) { diff --git a/Cake/Test/TestCase/ORM/TableTest.php b/Cake/Test/TestCase/ORM/TableTest.php index 3c654056b09..b920de8afc4 100644 --- a/Cake/Test/TestCase/ORM/TableTest.php +++ b/Cake/Test/TestCase/ORM/TableTest.php @@ -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')); } /** @@ -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')); + } + }