-
-
Notifications
You must be signed in to change notification settings - Fork 72
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
Make setCallback() add to previous callbacks #473
Make setCallback() add to previous callbacks #473
Conversation
c0c5522
to
36cdde2
Compare
setCallback() replaces previous callbacks making you have to repeat any logic in the "base case' definition callback if you have a callback on one of the groups. See issue thephpleague#472
36cdde2
to
6d55131
Compare
Thanks for preparing this. This is certainly a breaking change, so can't be merged as is, and it is not obvious how we should implement |
$oldCallback = $this->callback; | ||
$this->callback = function ($model, $saved) use ($oldCallback, $callback) { | ||
// If the old callback returns false, the whole thing should return false. | ||
return $oldCallback($model, $saved) !== false && $callback($model, $saved) !== false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Returning false to exit the callback chain is incompatible with the fact that false also determines if a save should go ahead or not. What if someone wants to exit the callback chain but still wants another persit to run?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I understand correctly, the current implementation will not persist the model if the callback returns false? So this line should make it so that if any of the callbacks return false, the model won't be persisted. Would you rather that all callbacks are called regardless of whether a prior one "failed" and then return false if any of them failed afterwards to prevent it being persisted?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I understand correctly, the current implementation will not persist the model if the callback returns false?
No, it will not persist the model again. Possibly someone might want the model to be updated in the database again after their callback does something, but does not want the remaining callbacks to be executed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, okay.
So I should change it to something like
$this->callback = function ($model, $saved) use ($oldCallback, $callback) {
$oldResult = $oldCallback($model, $saved);
$newResult = $callback($model, $saved);
return $oldResult !== false && $newResult !== false;
};
So that all the callbacks are always called, and returning false in any of them will prevent the second persist.
Then if we move this to addCallback()
, then you can still use setCallback()
to override all the other callbacks if you don't want them called.
If we added an |
I'm going to close this and submit another pull request that implements addCallback() to maintain backwards compatibility. |
setCallback() replaces previous callbacks making you have to repeat any logic in the "base case' definition callback if you have a callback on one of the groups.
See issue #472
A better solution may be to add a
addCallback
method instead of modifyingsetCallback()
.