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

Dev: Model needs new method - findOrCreate #5163

Open
lonnieezell opened this issue Oct 1, 2021 · 10 comments
Open

Dev: Model needs new method - findOrCreate #5163

lonnieezell opened this issue Oct 1, 2021 · 10 comments
Labels
dev hacktoberfest Issues ready for someone to tackle during Hacktoberfest missing feature Reported issue which is not a bug but needs to be implemented

Comments

@lonnieezell
Copy link
Member

lonnieezell commented Oct 1, 2021

The Model should provide a new convenience method, findOrCreate that can be given a set of criteria to locate within the db table, or create a new record using that data and an additional set of data that was provided.

Must work with all currently supported databases.

Undecided which version would work fit the project best, so am open to discussion on this one:

$userModel->where('email', '[email protected]')->findOrCreate(['key' => 'value', ...]);

// or

$userModel->findOrCreate(['existing_key' => 'value'], ['new_key' => 'value']);

References:

@lonnieezell lonnieezell added dev hacktoberfest Issues ready for someone to tackle during Hacktoberfest labels Oct 1, 2021
@iRedds
Copy link
Collaborator

iRedds commented Oct 1, 2021

Either your example is incorrect or the concept is lame.
You are talking about implementing a method in a model, but the example shows working with the BaseBuilder class.

In the CI paradigm, the BaseBuilder class provides simple basic methods for base operations (write/read). The new method in this case looks like sugar for sequential operations (read/read + write + read), which is contrary to the paradigm.

Model and BaseBuilder do not have create method, but insert method. This means that the new method must be named findOrInsert.

The second version is more compact, but it seems to me that the first parameter should also accept a string so that you can specify a key from the second palarament.

$data = ['a' => 1, 'b' => 2];
->findOrCreate('a', $data); // IMHO it looks better than
->findOrCreate(['a' => $data['a']), $data); 

@kenjis
Copy link
Member

kenjis commented Oct 1, 2021

@lonnieezell
Copy link
Member Author

CakePHP has findOrCreate().

Laravel also has it. The difference with Cake's is that we can't always assume an entity is being used, but it's interesting to be able to use one. I don't think that's necessary for this first pass, though.

@iRedds you're right - the example code was wrong. I'll fix that. And I have no problem with it being called findOrInsert.

@kenjis
Copy link
Member

kenjis commented Oct 2, 2021

@lonnieezell Do you mean firstOrCreate in Laravel?
https://laravel.com/docs/8.x/eloquent#retrieving-or-creating-models

@kenjis kenjis added the missing feature Reported issue which is not a bug but needs to be implemented label Apr 21, 2022
@Valkhan
Copy link

Valkhan commented Nov 12, 2022

@kenjis
Copy link
Member

kenjis commented Nov 13, 2022

No. save() is to create or update.

@datamweb
Copy link
Contributor

This feature is needed, I recently noticed its absence.

For example, the user may want to log in with Google, so the system first checks whether the user exists or not, and if there is, it is logged in.

If it does not exist, the new user is first registered and then logged in.

If this issue refers to this issue, I agree to add it.

@kenjis
Copy link
Member

kenjis commented Feb 21, 2024

@lonnieezell
Copy link
Member Author

Actually, that article reinforces the idea that we should provide a solution. This is a fairly common pattern developers have to do. I'll admit I never coded for race conditions in those situations. I'm sure most people don't.

@kenjis
Copy link
Member

kenjis commented Feb 22, 2024

Okay.

References:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dev hacktoberfest Issues ready for someone to tackle during Hacktoberfest missing feature Reported issue which is not a bug but needs to be implemented
Projects
None yet
Development

No branches or pull requests

5 participants