-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
Make Table::findOrCreate() more flexible #8671
Comments
Would you like to submit a pull request for this? |
@lorenzo I can do that, but you know that I'll never write code before discussing it to avoid wasting my time with something that is not accepted at the end. 😄 So let's discuss how to implement this right. My main concern right now is how to keep it BC and allow all three proposed solutions. I think changing the method signature and checking for the kind of the first passed argument could make this possible.
Any better or additional ideas? |
Isn't a query object 'better' than a finder? $articles->findOrCreate($articles->find('published'), function () { ... }); |
|
@markstory I'm OK with just the query object as well, just one question: How do we pass the search conditions then? Maybe like this?
And make the search optional? The problem I ran into in my case was that the $search is used for the entity / save as well but that I had to prefix the fields with the alias, which then made the entity fail. So I ended up with doing this in my case. $project = $this->Projects->findOrCreateProject([
'Projects.is_draft' => true,
'Projects.company_id' => $companyId,
'Projects.profile_id' => $profileId
], function($entity) use($companyId, $profileId) {
$entity->name = '';
$entity->is_draft = true;
$entity->profile_id = $profileId;
$entity->company_id = $companyId;
}); Edit: I ended up implementing the string check for search as well but a little different. Instead of checking for a finder with that name I'm checking for method exists. This allows us to keep the actual code and method that will return the query in the model. See the PR. |
Closing as the proposed changes are in a pull request form. |
This is a (multiple allowed):
What you did
Wanted to implement findOrCreate() and realized it is limited on customizing the find() call.
Expected Behavior
I would like to be able to define my own finder or query object.
Actual Behavior
I can only pass conditions, not define contains or use a finder as first arg.
Suggestion
Allow the first arg to be a query object or a string that is used as a finder.
The only problem I see is that the method has just 2 args for now, but it would be useful to have another one to pass something to the options of the finder.
The text was updated successfully, but these errors were encountered: