-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Comments
Either your example is incorrect or the concept is lame. 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.
|
CakePHP has |
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 |
@lonnieezell Do you mean |
Isn't that what save() does? https://codeigniter4.github.io/CodeIgniter4/models/model.html?highlight=save#save |
No. save() is to create or update. |
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. |
This method seems to be extremely complex. Do we really need this? |
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. |
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:
References:
The text was updated successfully, but these errors were encountered: