Bug report
Bug report
Initially I thought the bug I found today is similar to this one: https://datatables.net/forums/discussion/42850/postsubmit-event-not-always-raised-in-editor-1-6-3#latest
But it is actually not the same. It is pretty serious too and I haven't found a work around yet. I have a unique index on two columns in a very simple table consisting of only five columns: id, currency, date, rate, update_time. The unique index is on currency and date. A duplicate key situation could be created through both an insert and an update.
The application crashes on a simple SQL dup key when I use "postSubmit"
The event is being triggered but the json object is a null value!
An SQL error occurred: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'SEK-2010-02-11 00:00:00' for key 'UniqueIndexForex'
Fatal error: Call to a member function insertId() on boolean in E:\xampp\htdocs\lgf\DataTables\php\Editor\Editor.php on line 1624
This is my post submit statement which doesn't work any more:
forexEditor
.on( 'postSubmit', function ( e, json, data, action ) {
if (json.error) {
var sqlDupKey = json.error;
sqlDupKey = sqlDupKey.substring(0, 15 )
if (sqlDupKey == 'SQLSTATE[23000]') {
json.error = 'Sorry, this rate already exists!';
}
}
});
For the sake of completeness: If there is no SQL error the event is also triggered and the JSON object is not null. So something is wrong with Editor's error handling I guess.
If I remove the 'postSubmit' event I get the usual system error message with a link to this
https://datatables.net/manual/tech-notes/12
The crash in Editor is therefore caused by the 'postSubmit' event.
This question has an accepted answers - jump to answer
Answers
@ Allan: Would you have a fix or a work around for me, please?
In my own coding I do this to avoid a dup key but I don't know how to do this with data tables.
Hi,
I'm not quite clear on how the
postSubmit
Javascript event handler is causing that SQL error? Or perhaps I'm misunderstanding what you mean by "crash" - I'm assuming that means the error?It sounds like there is some validation missing for the edited data. You would need to check that the submitted data is not going to cause a conflict before inserting it - a global validator could be used for that since it needs to consider the values from two different fields.
Apologies if I've got the wrong end of the stick with this one.
Allan
postSubmit does not cause the SQL error. That is caused because the record already exists. I defined the unique index in the database so that the database does the work for me and I don't have to check for a duplicate key myself.
I use postSubmit in order to avoid the display of the usual system error message. Instead I want to display 'Sorry, this rate already exists!' This is a very simple way of checking for a duplicate key. Much easiere than the coding I show above. In fact it does not involve any coding. All you need to do is check for SQLSTATE[23000] and display the right message.
This worked perfectly with the previous Editor version. But now Editor crashes upon the SQL error. This is the error message from Editor upon duplicate key (see above as well).
Fatal error: Call to a member function insertId() on boolean in E:\xampp\htdocs\lgf\DataTables\php\Editor\Editor.php on line 1624
So Editor.php crashes on line 1624 which it didn't do in the previous version. Editor should definitely not crash upon an SQL error. It should simply get back with the SQL error as it did before.
If I remove the postSubmit Editor does not crash any longer. It simply display the usual system error message. For that reason I believe that the postSubmit causes Editor to crash.
Allan, forget this please: "If I remove the postSubmit Editor does not crash any longer. It simply display the usual system error message. For that reason I believe that the postSubmit causes Editor to crash."
Unfortunately this does not make any sense because the error is in the Editor.php and not in Javascript. I tested once again with the removed postSubmit.
The system error message is displayed as above. But of course Editor.php crashes as well. Same error as with postSubmit.
As you can see the SQL error comes first (which is a "wanted" error if you know what I mean) and then Editor.php crashes. The JavaScript error using postSubmit is only a consequence of the Editor.php bug because of this bug the json object does not get returned to JavaScript.
I went back to using the Editor php libraries of version 1.6.2 - and everything works perfectly fine as you can see on the attached screen shot. When I try to enter the Australian $ exchange rate to the Euro for July 7 again I get the message "Sorry, this rate already exists!"
My work around for now is to use Editor 1.6.2
Do you have
tryCatch( false )
in your Editor PHP code by any chance?As you say, Editor should be catching such an error.
Allan
No, I don't have it anywhere! Just checked. Editor 1.6.2 catches the error and returns it to Javascript as "json.error".
I'm not quite sure I understand where the PHP error statement about the insertId is coming from in that case I'm afraid. is that in the error log, or the returned JSON?
Allan
No json is being returned. Hence the Javascript error on postSubmit.
The error statement is in the error log.
I attached another screen shot which has all errors in it:
1. The "wanted" SQL error (duplicate key)
2. The PHP error in Editor.php
3. The Javascript error because of the missing json response
Ah!! I see. Okay, thanks for the clarification.
That's a problem. Editor's Javascript is always expecting JSON in the response. Anything else should result in the generic "System error" message.
The libraries should be returning something like:
if an SQL error is being caused.
Let me try setting something up locally to reproduce that and I'll get back to you.
Allan
Thanks Allan.
I think we are in sync about this. Since this all works in Editor 1.6.2 it is hopefully not too hard for you to figure this out.
This is what Editor 1.6.2 returns as "json.error":
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'AUD-2017-07-07 00:00:00' for key 'UniqueIndexForex'
{"fieldErrors":[],"error":"SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'AUD-2017-07-07 00:00:00' for key 'UniqueIndexForex'","data":[],"ipOpts":[],"cancelled":[]}
I've been using Editor since version 1.5 and this has always worked like this.
Could you try changing the following line in
php/Database/Driver/Mysql/Query.php
:to be:
That will resolve it and the error message will be returned in valid JSON.
Thanks,
Allan
ok, that works! Many thanks!
You changed the message compared to Editor 1.6.2
Previously you only displayed the error message without displaying "An SQL error occured: ". But that is no problem I will make a small change in my exception handling to make it more flexiblie regarding the position of the SQLSTATE to avoid future issues.
This is how I changed the handling right now. Works.