cms icon indicating copy to clipboard operation
cms copied to clipboard

[5.x] Add `findOrNew`, `firstOrCreate`, `createOrUpdate` (& related methods) methods to Entry & Term query builders

Open godismyjudge95 opened this issue 1 year ago • 7 comments

This PR adds a few helper methods to the repositories/facades: firstOrCreate and updateOrCreate. These methods exist on Laravel eloquent models: https://laravel.com/docs/11.x/eloquent#retrieving-or-creating-models https://laravel.com/docs/11.x/eloquent#updates

As a result, this PR attempts to replicate that functionality but for Statamic's content types.

I am marking this as a draft as I would like to implement this for all reasonable facades/repositories. If I am off track on the implementation feel free to point it out.

godismyjudge95 avatar Apr 03 '24 15:04 godismyjudge95

I removed the createOrFirst method as Statamic does not throw an exception when the entry/term already exists. This means the method would always create/update and never just return the entry/term.

godismyjudge95 avatar Apr 05 '24 19:04 godismyjudge95

@duncanmcclean unless you had any other comments on major portions of this, I think it is ready for review?

godismyjudge95 avatar Apr 05 '24 20:04 godismyjudge95

Thanks! We'll review this when we can.

duncanmcclean avatar Apr 05 '24 20:04 duncanmcclean

Thanks for your work on this pull request!

I've made a couple of tweaks to the tests and implemented a few of the other methods missing from our query builder implementation.

I've also opened #9976 which includes some of the methods useful for all of Statamic's query builders.

Wow thank you for all of these improvements! This will help a ton in our use cases.

godismyjudge95 avatar Apr 30 '24 14:04 godismyjudge95

Thanks @godismyjudge95 for dedicating your time to this, such a great PR.

I'd love to see this merged 🙏🏻

johncarter- avatar Dec 13 '24 10:12 johncarter-