Skip to content

Commit a8e410e

Browse files
committed
fix save with habtm returning false
1 parent 2546b79 commit a8e410e

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

lib/Cake/Model/Model.php

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1784,7 +1784,7 @@ public function save($data = null, $validate = true, $fieldList = array()) {
17841784
if (empty($this->data[$this->alias][$this->primaryKey])) {
17851785
unset($this->data[$this->alias][$this->primaryKey]);
17861786
}
1787-
$fields = $values = array();
1787+
$joined = $fields = $values = array();
17881788

17891789
foreach ($this->data as $n => $v) {
17901790
if (isset($this->hasAndBelongsToMany[$n])) {
@@ -1843,24 +1843,26 @@ public function save($data = null, $validate = true, $fieldList = array()) {
18431843
}
18441844
}
18451845

1846-
if (!empty($joined) && $success === true) {
1847-
$this->_saveMulti($joined, $this->id, $db);
1848-
}
1849-
1850-
if ($success && $count === 0) {
1851-
$success = false;
1846+
if ($success) {
1847+
if (!empty($joined)) {
1848+
$this->_saveMulti($joined, $this->id, $db);
1849+
} else if ($count === 0) {
1850+
$success = false;
1851+
}
18521852
}
18531853

1854-
if ($success && $count > 0) {
1855-
if (!empty($this->data)) {
1856-
if ($created) {
1857-
$this->data[$this->alias][$this->primaryKey] = $this->id;
1854+
if ($success) {
1855+
if ($count > 0) {
1856+
if (!empty($this->data)) {
1857+
if ($created) {
1858+
$this->data[$this->alias][$this->primaryKey] = $this->id;
1859+
}
18581860
}
1859-
}
18601861

1861-
if ($options['callbacks'] === true || $options['callbacks'] === 'after') {
1862-
$event = new CakeEvent('Model.afterSave', $this, array($created, $options));
1863-
$this->getEventManager()->dispatch($event);
1862+
if ($options['callbacks'] === true || $options['callbacks'] === 'after') {
1863+
$event = new CakeEvent('Model.afterSave', $this, array($created, $options));
1864+
$this->getEventManager()->dispatch($event);
1865+
}
18641866
}
18651867

18661868
if (!empty($this->data)) {

lib/Cake/Test/Case/Model/ModelWriteTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1824,7 +1824,9 @@ public function testSaveHabtmNoPrimaryData() {
18241824

18251825
$data = array('Item' => array('Item' => array(1, 2)));
18261826
$TestModel->id = 2;
1827-
$TestModel->save($data);
1827+
$result = $TestModel->save($data);
1828+
$this->assertTrue((bool)$result);
1829+
18281830
$result = $TestModel->findById(2);
18291831
$result['Item'] = Hash::sort($result['Item'], '{n}.id', 'asc');
18301832
$expected = array(

0 commit comments

Comments
 (0)