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

Adding increment and decrement methods #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

AdamSEY
Copy link

@AdamSEY AdamSEY commented May 20, 2019

While working with NodeJS I found some super popular package called knex.js using increment and decrement methods and thought about adding them to this great library

@ocram
Copy link
Contributor

ocram commented May 23, 2019

Thank you very much!

The question is whether

$db->increment(
    'users',
    [ 'balance' => 25 ],
    [ 'username' => 'Adam' ]
);

is considerably shorter and more convenient than

$db->exec(
    'UPDATE users SET balance = balance + ? WHERE username = ?',
    [
        25,
        'Adam'
    ]
);

which is already supported today. The answer seems to be yes, it is. Not by a large margin, but still. So it would make sense to add it.

I’m not sure if we need separate increment and decrement methods when we can just use the sign of the numbers supplied in the second argument. The two methods could both simply be named addition. (We could even add multiplication as well.) But if we want a verb as the method name here, I can’t think of any better name (that is still unambiguous) than increment and decrement.

By the way, I also noticed that in the original Node.js package “knex.js”, this is part of a query builder, not a standalone method as it would be here. That should not really matter, although you cannot create complex conditions without a query builder.

@AdamSEY
Copy link
Author

AdamSEY commented May 23, 2019

Yes I think with the query builder its much easier, maybe in the future we can work on a query builder, I don't think it's that hard with PHP.

actually I thought about using a variable to determine the sign, but I felt it's a little bit unreadable and ambiguous that's way I made a private function for that matter.

Those two methods might be some unneeded extras, but I found them fun and useful 👍
Feel free to improve the current code or close the pull request, that's totally fine :)

@AdamSEY AdamSEY changed the title Added increment and decrement methods Adding increment and decrement methods May 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants