From a8e410ee20008c2fb6e38867b2da59d1ccf63da9 Mon Sep 17 00:00:00 2001 From: Haithem BEN GHORBAL Date: Wed, 28 May 2014 18:17:16 +0200 Subject: [PATCH 1/4] fix save with habtm returning false --- lib/Cake/Model/Model.php | 36 +++++++++++---------- lib/Cake/Test/Case/Model/ModelWriteTest.php | 4 ++- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/lib/Cake/Model/Model.php b/lib/Cake/Model/Model.php index 76160262e..664016283 100644 --- a/lib/Cake/Model/Model.php +++ b/lib/Cake/Model/Model.php @@ -1784,7 +1784,7 @@ class Model extends Object implements CakeEventListener { 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 @@ class Model extends Object implements CakeEventListener { } } - if (!empty($joined) && $success === true) { - $this->_saveMulti($joined, $this->id, $db); - } - - if ($success && $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 (!empty($joined)) { + $this->_saveMulti($joined, $this->id, $db); + } else if ($count === 0) { + $success = false; } + } - if ($options['callbacks'] === true || $options['callbacks'] === 'after') { - $event = new CakeEvent('Model.afterSave', $this, array($created, $options)); - $this->getEventManager()->dispatch($event); + 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 (!empty($this->data)) { diff --git a/lib/Cake/Test/Case/Model/ModelWriteTest.php b/lib/Cake/Test/Case/Model/ModelWriteTest.php index 6acfd3c51..cd427a9aa 100644 --- a/lib/Cake/Test/Case/Model/ModelWriteTest.php +++ b/lib/Cake/Test/Case/Model/ModelWriteTest.php @@ -1824,7 +1824,9 @@ class ModelWriteTest extends BaseModelTest { $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( From d4a0883cb973cc896045287fce3378587fa98c39 Mon Sep 17 00:00:00 2001 From: Haithem BEN GHORBAL Date: Wed, 28 May 2014 18:31:03 +0200 Subject: [PATCH 2/4] fix typo/coding style --- lib/Cake/Model/Model.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Cake/Model/Model.php b/lib/Cake/Model/Model.php index 664016283..8fd9cc399 100644 --- a/lib/Cake/Model/Model.php +++ b/lib/Cake/Model/Model.php @@ -1846,7 +1846,7 @@ class Model extends Object implements CakeEventListener { if ($success) { if (!empty($joined)) { $this->_saveMulti($joined, $this->id, $db); - } else if ($count === 0) { + } elseif ($count === 0) { $success = false; } } From 67af8b37db22c8aef32df926f8c0227458ef626c Mon Sep 17 00:00:00 2001 From: Haithem Ben Ghorbal Date: Wed, 28 May 2014 23:53:47 +0200 Subject: [PATCH 3/4] clean code and remove unneeded checks --- lib/Cake/Model/Model.php | 58 ++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/lib/Cake/Model/Model.php b/lib/Cake/Model/Model.php index 8fd9cc399..69a39af2c 100644 --- a/lib/Cake/Model/Model.php +++ b/lib/Cake/Model/Model.php @@ -1807,6 +1807,11 @@ class Model extends Object implements CakeEventListener { } } + if (empty($fields) && empty($joined)) { + $this->whitelist = $_whitelist; + return false; + } + $count = count($fields); if (!$exists && $count > 0) { @@ -1843,38 +1848,33 @@ class Model extends Object implements CakeEventListener { } } - if ($success) { - if (!empty($joined)) { - $this->_saveMulti($joined, $this->id, $db); - } elseif ($count === 0) { - $success = false; - } - } - - 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 (!empty($this->data)) { - $success = $this->data; - } - - $this->_clearCache(); - $this->validationErrors = array(); - $this->data = false; + if ($success && !empty($joined)) { + $this->_saveMulti($joined, $this->id, $db); } $this->whitelist = $_whitelist; + + if (!$success) { + return $success; + } + + if ($count > 0) { + 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); + } + } + + $success = $this->data; + + $this->_clearCache(); + $this->validationErrors = array(); + $this->data = false; + return $success; } From 35c2a7ef3153becdc76f2b2cb5097524bb81af13 Mon Sep 17 00:00:00 2001 From: Haithem Ben Ghorbal Date: Thu, 29 May 2014 02:36:13 +0200 Subject: [PATCH 4/4] restore previous (erroneous ?) behaviour --- lib/Cake/Model/Model.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/Cake/Model/Model.php b/lib/Cake/Model/Model.php index 69a39af2c..31fd36459 100644 --- a/lib/Cake/Model/Model.php +++ b/lib/Cake/Model/Model.php @@ -1869,7 +1869,9 @@ class Model extends Object implements CakeEventListener { } } - $success = $this->data; + if (!empty($this->data)) { + $success = $this->data; + } $this->_clearCache(); $this->validationErrors = array();