Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.6 fix save() with habtm returning error #3590

Merged
merged 4 commits into from
May 29, 2014
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions lib/Cake/Model/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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])) {
Expand Down Expand Up @@ -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);
} elseif ($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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if those 4 nested levels of ifs are really necessary..
at least the last two could probably be combined.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah looks a bit too much

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about this:

$this->whitelist = $_whitelist;

if (!$success) {
    return $success;
}

if ($count > 0) {
    if (!empty($this->data) && $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;

return $success;

$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)) {
Expand Down
4 changes: 3 additions & 1 deletion lib/Cake/Test/Case/Model/ModelWriteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down