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

Conversation

Haititi
Copy link
Contributor

@Haititi Haititi commented May 28, 2014

bug found while handling PR 3516.
#3516

$Model->save($data) may return false if the given $data to save does not contain some $Model related data.

if ($success) {
if (!empty($joined)) {
$this->_saveMulti($joined, $this->id, $db);
} else if ($count === 0) {
Copy link
Member

Choose a reason for hiding this comment

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

elseif

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;

@Haititi
Copy link
Contributor Author

Haititi commented May 28, 2014

this new commit should hopefully allow better readability.
some checks was not needed as the result was obvious.

@Haititi
Copy link
Contributor Author

Haititi commented May 29, 2014

Erf... each time I fix a bug, I find another one...

  • Test testSaveAllNestedSaveAll and testSaveManyNestedSaveMany are using some TransactionTestModel and TransactionManyTestModel which define an 'afterSave' callback to do another save.
  • The second save succeed and set $this->data = false; before returning the array of data saved.
  • Then, the first save finishs its process but fails due to the returning data set to false previously...

I will restore the previous behaviour, but that will imply that user will only be able to retrieve 'true/false' instead of the array of saved data.

@markstory markstory added this to the 2.6.0 milestone May 29, 2014
@markstory
Copy link
Member

Seems reasonable to me.

@Haititi
Copy link
Contributor Author

Haititi commented May 29, 2014

when do you think this will be merged ? I was thinking I will wait for the merge and rebase my branch used for #3516 to resolve the remaining problems...

markstory added a commit that referenced this pull request May 29, 2014
2.6 fix save() with habtm returning error
@markstory markstory merged commit a135378 into cakephp:2.6 May 29, 2014
@Haititi Haititi deleted the 2.6-fix-habtm branch May 29, 2014 15:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants