Welcome Guest, Not a member yet? Register   Sign In
You must use the "set" method to update an entry on empty insert
#1

[eluser]Alsciende[/eluser]
Hello,

CI throwed the Database Error :

Quote:You must use the "set" method to update an entry

because I was trying to insert() an empty array in a table. Test case:

Code:
$this->db->insert('mytable', array());

It seems to me that the error message is misleading. Since my actual code wasn't as dumb as the above test case, it took me a while to figure out that my array was empty.
#2

[eluser]WanWizard[/eluser]
Always validate your data. You would have caught the case where there was nothing to insert.

I agree with you that it's not a good idea to display an UPDATE error on an INSERT query, something that needs to be addressed.
#3

[eluser]Alsciende[/eluser]
Don't assume too much :-) My array wasn't empty because of some user input, but because I didn't pass it by reference to some custom helper function that would have filled it up.

Anyway, thanks for the thumbs up. I hope a fix will be found.
#4

[eluser]WanWizard[/eluser]
I didn't assume anything, and I didn't mention user input.

I said validate your data. Design unit tests. Run them. A lot of errors come not from user input, but from developers mistakes (which is why user input can be an issue as well btw...)

I don't know what kind of fix you're referring to, but I expect no code will be changed, as this will be cataloged as user-error.
I'm only suggesting to modify the error message to something like "You must specify values to insert a new entry"...
#5

[eluser]Alsciende[/eluser]
Wow I'm certainly not going to post here again anytime soon.

Good luck with your users, and with their feedback. And keep providing confusing error messages, that's the way to go. Update for Insert, Insert for Update, who cares? It's the user's fault, anyway.

Cheers
#6

[eluser]InsiteFX[/eluser]
Well don't you think you should have checked your array before you tried working with it!

Validating input is one of the top priorities in programming.

Never trust user input and in this case you were the user!

Good luck in your programming career.

InsiteFX
#7

[eluser]Alsciende[/eluser]
For the record, I never argued with the fact that CI yelled at me. It was perfectly fine to yell at me, because what I was doing was wrong.

I just pointed out the fact that the error message was misleading, since it talked about an Update operation when I was asking for an Insert operation, and asked me to do a set that I was already doing (even if with an empty array, I was actually calling the set method). When you don't know what is wrong with your code and you're still trying to get used to a new (and great, btw, even if I'm a lot less enthusiastic now) framework, mistakes can be hard to detect. That's all. I was doing something wrong, and CI thrown an error that was arguably inaccurate - at least from a stupid user's point of view, since I'm sure there's a perfectly valid reason for this message.

edit: I did try $this->db->set($data)->insert('mytable'), even if I didn't mention it in the initial post.
#8

[eluser]Alsciende[/eluser]
[quote author="WanWizard" date="1300747108"]
I don't know what kind of fix you're referring to, but I expect no code will be changed, as this will be cataloged as user-error.
I'm only suggesting to modify the error message to something like "You must specify values to insert a new entry"...[/quote]

That's exactly the kind of fix I was referring to. I'm actually wondering what made you think otherwise.




Theme © iAndrew 2016 - Forum software by © MyBB