From 1528b5c3820ca97acc989438f283d74b125cc4af Mon Sep 17 00:00:00 2001 From: mark_story Date: Sun, 16 Dec 2012 13:18:59 -0500 Subject: [PATCH] Don't return true when there is nothing to save. Model::save() should not return true when no work has been done. Fixes #3469 --- lib/Cake/Model/Model.php | 4 ++++ lib/Cake/Test/Case/Model/ModelWriteTest.php | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/lib/Cake/Model/Model.php b/lib/Cake/Model/Model.php index 102c10e73..0ef57939e 100644 --- a/lib/Cake/Model/Model.php +++ b/lib/Cake/Model/Model.php @@ -1749,6 +1749,10 @@ class Model extends Object implements CakeEventListener { $this->_saveMulti($joined, $this->id, $db); } + if ($success && $count === 0) { + $success = false; + } + if ($success && $count > 0) { if (!empty($this->data)) { if ($created) { diff --git a/lib/Cake/Test/Case/Model/ModelWriteTest.php b/lib/Cake/Test/Case/Model/ModelWriteTest.php index 1a015ef42..d901063eb 100644 --- a/lib/Cake/Test/Case/Model/ModelWriteTest.php +++ b/lib/Cake/Test/Case/Model/ModelWriteTest.php @@ -24,6 +24,24 @@ require_once dirname(__FILE__) . DS . 'ModelTestBase.php'; */ class ModelWriteTest extends BaseModelTest { +/** + * Test save() failing when there is no data. + * + * @return void + */ + public function testInsertNoData() { + $this->loadFixtures('Bid'); + $Bid = ClassRegistry::init('Bid'); + + $this->assertFalse($Bid->save()); + + $result = $Bid->save(array('Bid' => array())); + $this->assertFalse($result); + + $result = $Bid->save(array('Bid' => array('not in schema' => 1))); + $this->assertFalse($result); + } + /** * testInsertAnotherHabtmRecordWithSameForeignKey method *