diff --git a/lib/Cake/Model/Model.php b/lib/Cake/Model/Model.php index 76160262ef4..66401628340 100644 --- a/lib/Cake/Model/Model.php +++ b/lib/Cake/Model/Model.php @@ -1784,7 +1784,7 @@ public function save($data = null, $validate = true, $fieldList = array()) { if (empty($this->data[$this->alias][$this->primaryKey])) { unset($this->data[$this->alias][$this->primaryKey]); } - $fields = $values = array(); + $joined = $fields = $values = array(); foreach ($this->data as $n => $v) { if (isset($this->hasAndBelongsToMany[$n])) { @@ -1843,24 +1843,26 @@ public function save($data = null, $validate = true, $fieldList = array()) { } } - if (!empty($joined) && $success === true) { - $this->_saveMulti($joined, $this->id, $db); - } - - if ($success && $count === 0) { - $success = false; + if ($success) { + if (!empty($joined)) { + $this->_saveMulti($joined, $this->id, $db); + } else if ($count === 0) { + $success = false; + } } - if ($success && $count > 0) { - if (!empty($this->data)) { - if ($created) { - $this->data[$this->alias][$this->primaryKey] = $this->id; + if ($success) { + if ($count > 0) { + if (!empty($this->data)) { + if ($created) { + $this->data[$this->alias][$this->primaryKey] = $this->id; + } } - } - if ($options['callbacks'] === true || $options['callbacks'] === 'after') { - $event = new CakeEvent('Model.afterSave', $this, array($created, $options)); - $this->getEventManager()->dispatch($event); + if ($options['callbacks'] === true || $options['callbacks'] === 'after') { + $event = new CakeEvent('Model.afterSave', $this, array($created, $options)); + $this->getEventManager()->dispatch($event); + } } if (!empty($this->data)) { diff --git a/lib/Cake/Test/Case/Model/ModelWriteTest.php b/lib/Cake/Test/Case/Model/ModelWriteTest.php index 6acfd3c5119..cd427a9aa98 100644 --- a/lib/Cake/Test/Case/Model/ModelWriteTest.php +++ b/lib/Cake/Test/Case/Model/ModelWriteTest.php @@ -1824,7 +1824,9 @@ public function testSaveHabtmNoPrimaryData() { $data = array('Item' => array('Item' => array(1, 2))); $TestModel->id = 2; - $TestModel->save($data); + $result = $TestModel->save($data); + $this->assertTrue((bool)$result); + $result = $TestModel->findById(2); $result['Item'] = Hash::sort($result['Item'], '{n}.id', 'asc'); $expected = array(