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

Return Affected Rows #12

Closed
votemike opened this issue Jan 18, 2017 · 3 comments
Closed

Return Affected Rows #12

votemike opened this issue Jan 18, 2017 · 3 comments

Comments

@votemike
Copy link
Contributor

Would it be possible to somehow grab the number of rows affected? At the moment all the methods return a bool whereas I'd like to know if an INSERT was actually done or ignored. It would be easy to use Laravel's affectingStatement() method, but that would change the API of your code.

Would it be possible to make this breaking change and bump the version number?

Or possibly save the number of affected rows and add a method to grab that value.

I should be able to do the work if you like.

@yadakhov
Copy link
Owner

Sure. Do a PR and I'll merge.

Note that affected rows counts as 2 for duplicates.

http://stackoverflow.com/questions/3747314/why-are-2-rows-affected-in-my-insert-on-duplicate-key-update

yadakhov added a commit that referenced this issue Jan 19, 2017
@stebogit
Copy link

stebogit commented Nov 2, 2017

@votemike I'm not sure how to use this feature when inserting multiple rows at once, and I believe there is at least an imprecision in the docs.
The current documentation says:

/*
* @return int 0 if row is not changed, 1 if row is inserted, 2 if row is updated
*/
public static function insertOnDuplicateKey(array $data, array $updateColumns = null)

however with multiple inserts/updates the output is the sum of the single affected-rows values.
Now I get that for a single operation 0-1-2 values help to discriminate what happened, but here, from the sum, how do I define how many rows have been inserted and how many updated?

It would be nice to return an array like

[ 'updates' => 2, 'inserts' => 17 ]

but is it even possible, I mean does MySQL even allow to discriminate between insert/update in a big single statement like the one the method creates?

Thanks, and thank you @yadakhov for the great tool! 👍

@votemike
Copy link
Contributor Author

votemike commented Nov 2, 2017

Wow, it's been a while since I did this work. @vifer do you still work with this? Any idea about @stebogit 's question?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants