docs icon indicating copy to clipboard operation
docs copied to clipboard

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

Open mehov opened this issue 10 months ago • 4 comments

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 avatar Feb 13 '25 14:02 mehov

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

mehov avatar Feb 13 '25 18:02 mehov

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 avatar Feb 15 '25 21:02 markstory

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 avatar Feb 19 '25 10:02 mehov

Todo:

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

(I hope it allows you to choose too?)

mehov avatar Feb 19 '25 10:02 mehov

This issue is stale because it has been open for 120 days with no activity. Remove the stale label or comment or this will be closed in 15 days

github-actions[bot] avatar Jun 20 '25 00:06 github-actions[bot]