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

Expected beforeSave() return value to let save go through? #7996

Open
mehov opened this issue Feb 13, 2025 · 4 comments
Open

Expected beforeSave() return value to let save go through? #7996

mehov opened this issue Feb 13, 2025 · 4 comments
Milestone

Comments

@mehov
Copy link
Contributor

mehov commented Feb 13, 2025

In some cases, a beforeSave() callback may want to let the save continue without changing the entity.

Documentation at https://book.cakephp.org/5/en/orm/table-objects.html#beforesave does not say what value to return early to accomplish that.

In my case if (something) {return;} works, but I'm not sure if that's the best/recommended way to do it. It could well be, as https://api.cakephp.org/5.0/class-Cake.ORM.Behavior.Translate.ShadowTableStrategy.html#beforeSave() says it's "Returns: void" by default.

Do you think it may be a good idea to explicitly clarify that in the Book for beforeSave() and maybe others?

@mehov
Copy link
Contributor Author

mehov commented Feb 13, 2025

Please also consider explicitly stating that if you modify an entity you don't have to return it, return; is enough

@markstory
Copy link
Member

I think giving some more detail on how to return early from the before callbacks makes sense. Any interest in getting a pull request started?

@markstory markstory added this to the 5.x milestone Feb 15, 2025
@mehov
Copy link
Contributor Author

mehov commented Feb 19, 2025

Sure!

return early from the before callbacks

I'm looking at https://book.cakephp.org/5/en/orm/table-objects.html#event-list and there's many others

Should I do all? Or some of them? Only before*?

I'm guessing "return; continues business as usual" is true for all of them?

@mehov
Copy link
Contributor Author

mehov commented Feb 19, 2025

Todo:

  • Model.initialize
  • Model.beforeMarshal
  • Model.afterMarshal
  • Model.beforeFind
  • Model.buildValidator
  • Model.buildRules
  • Model.beforeRules
  • Model.afterRules
  • Model.beforeSave
  • Model.afterSave
  • Model.afterSaveCommit
  • Model.beforeDelete
  • Model.afterDelete
  • Model.afterDeleteCommit

(I hope it allows you to choose too?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants