-
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
When/WhenNot methods for db in a trait #6574
Conversation
What do you think of this kind of code? function when ($when, callable $true, ?callable $false = null)
{
($when ? $true : $false ?? function(){})($this, $when);
}
function whenNot ($when, callable $true, ?callable $false = null)
{
when($when, $false ?? function(){}, $true);
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@iRedds I like the efficiency but I had to read it like five times to figure it out; I think @lonnieezell's version is much easier to understand without galaxy brain 😉 I do like |
@iRedds To be honest, the code is difficult to understand.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I follow @iRedds suggestion to make this one method with its inversion.
* | ||
* @return $this | ||
*/ | ||
public function whenNot($condition, callable $callback, ?callable $defaultCallback = null): self |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
whenNot
as the inverse of when
seems quite not natural for me. It's a bit off. How about other antonyms of when
:
- except
- unless
- if not
- lest
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The method expects the condition to evaluate to false
, which makes except
and unless
not make semantic sense. ifNot
as an opposite of when
feels wrong also. lest
feels like the condition should evaluate to true
to me, also.
whenNot
does at least match the pattern of other methods like where
/whereNot
. I'm open to suggestions but I'm not sure any of these fit, unless we change the whole method to suit the name.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
except
/unless
makes sense to me if the statement is read in a specific way. In the context of the DB builder, since I know the usage of when
then when I read its inverse (except
/unless
):
$builder = $this->db->table('users');
$user = model('Users')->find(1);
return $builder->except($user, fn ($query) { /* some code */ })->get();
In this example, I would read the code as "Except when $user
is truthy (or a found user record), let the $builder
execute the callable then get the DB result."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
whenNot
is easy to understand for non native English speakers.
It is logical. The when
whenNot
pair is easy to remember, because we already have where/whereNot.
except
is difficult to imagine as the opposite of when
.
@MGatner are we ok to merge this one? |
@lonnieezell It needs to be switched to 4.3. You can try this: git rebase --onto develop 4.3 when-trait Just be ready to I think the content is all good, though I do think @paulbalandan 's suggestion is good. Maybe he would add it as an actual GitHub suggested change? |
@MGatner It's already on 4.3 so we're good there. Just committed @paulbalandan suggestion. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me! Last comment is the negative name; I don't have a strong feeling either way.
Can you remove the merge commit 49f1e70 ? |
There is a coding style error. |
I think the likelihood of me messing something up trying to do that is pretty high, honestly. Seems we just squash and merge and the history is nice and clean that way, won't it be? |
Co-authored-by: MGatner <[email protected]> Co-authored-by: kenjis <[email protected]>
Co-authored-by: kenjis <[email protected]>
Co-authored-by: John Paul E. Balandan, CPA <[email protected]>
Okay, I rebased and force-pushed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hurrah!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Finally, good result, thank you all.😍
🥳 |
This replaces #6329. It adds a new
ConditionalTrait
to CodeIgniter that can be used to addwhen()
andwhenNot()
methods in various places in the framework. This PR adds it to the database layer for inline conditions to queries.Instead of:
you can now do:
Checklist: