Merge pull request #3590 from Haititi/2.6-fix-habtm

2.6 fix save() with habtm returning error
This commit is contained in:
Mark Story 2014-05-29 10:09:33 -04:00
commit a135378950
2 changed files with 25 additions and 19 deletions

View file

@ -1784,7 +1784,7 @@ class Model extends Object implements CakeEventListener {
if (empty($this->data[$this->alias][$this->primaryKey])) { if (empty($this->data[$this->alias][$this->primaryKey])) {
unset($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) { foreach ($this->data as $n => $v) {
if (isset($this->hasAndBelongsToMany[$n])) { if (isset($this->hasAndBelongsToMany[$n])) {
@ -1807,6 +1807,11 @@ class Model extends Object implements CakeEventListener {
} }
} }
if (empty($fields) && empty($joined)) {
$this->whitelist = $_whitelist;
return false;
}
$count = count($fields); $count = count($fields);
if (!$exists && $count > 0) { if (!$exists && $count > 0) {
@ -1843,25 +1848,26 @@ class Model extends Object implements CakeEventListener {
} }
} }
if (!empty($joined) && $success === true) { if ($success && !empty($joined)) {
$this->_saveMulti($joined, $this->id, $db); $this->_saveMulti($joined, $this->id, $db);
} }
if ($success && $count === 0) { $this->whitelist = $_whitelist;
$success = false;
if (!$success) {
return $success;
} }
if ($success && $count > 0) { if ($count > 0) {
if (!empty($this->data)) {
if ($created) { if ($created) {
$this->data[$this->alias][$this->primaryKey] = $this->id; $this->data[$this->alias][$this->primaryKey] = $this->id;
} }
}
if ($options['callbacks'] === true || $options['callbacks'] === 'after') { if ($options['callbacks'] === true || $options['callbacks'] === 'after') {
$event = new CakeEvent('Model.afterSave', $this, array($created, $options)); $event = new CakeEvent('Model.afterSave', $this, array($created, $options));
$this->getEventManager()->dispatch($event); $this->getEventManager()->dispatch($event);
} }
}
if (!empty($this->data)) { if (!empty($this->data)) {
$success = $this->data; $success = $this->data;
@ -1870,9 +1876,7 @@ class Model extends Object implements CakeEventListener {
$this->_clearCache(); $this->_clearCache();
$this->validationErrors = array(); $this->validationErrors = array();
$this->data = false; $this->data = false;
}
$this->whitelist = $_whitelist;
return $success; return $success;
} }

View file

@ -1824,7 +1824,9 @@ class ModelWriteTest extends BaseModelTest {
$data = array('Item' => array('Item' => array(1, 2))); $data = array('Item' => array('Item' => array(1, 2)));
$TestModel->id = 2; $TestModel->id = 2;
$TestModel->save($data); $result = $TestModel->save($data);
$this->assertTrue((bool)$result);
$result = $TestModel->findById(2); $result = $TestModel->findById(2);
$result['Item'] = Hash::sort($result['Item'], '{n}.id', 'asc'); $result['Item'] = Hash::sort($result['Item'], '{n}.id', 'asc');
$expected = array( $expected = array(