Page 1 of 1

Deleting a date from a record

Posted: Thu Jun 04, 2020 10:55 pm
by BenFranske
How can you delete a date (and leave it blank) for a record?

If you try to do this on a mariadb field with type DATE you will get an error:

Code: Select all

SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect date value: '' for column...
This appears to be a correct error. The SQL:

Code: Select all

UPDATE individuals SET `death_date` = '' WHERE `ID` = '3be018e16a985';
Is not valid. If the date is set to nothing the SQL should be like:

Code: Select all

UPDATE individuals SET `death_date` = NULL WHERE `ID` = '3be018e16a985';
Is there a way to get nuBuilder to create the correct SQL? Maybe a custom code PHP Before Save that can re-write the SQL statement?

Thanks!

Re: Deleting a date from a record

Posted: Fri Jun 05, 2020 5:12 am
by kev1n
Hi,

Use the PHP AS event and set the date to null if it is blank.
See here for an example: https://forums.nubuilder.cloud/viewtopic. ... 04&p=17522

Re: Deleting a date from a record

Posted: Mon Jun 08, 2020 3:20 am
by BenFranske
Because that's an AS event though nuBuilder will still try to set the date to an empty string and get an error back from SQL though won't it? That will end up in the nuDebug log each time, etc.?

Is there a way to check and modify the SQL query in a BS event so that the incorrect SQL never even hits the server? I'm unclear if there is a way to modify the SQL that is going to be sent in a BS event but it seems like that would be a better fix if it could be done.

Or maybe a way to have nuBuilder add the EMPTY_STRING_IS_NULL option to sql_mode it's using before running the query?

Re: Deleting a date from a record

Posted: Mon Jun 08, 2020 6:45 am
by kev1n
You could try this fix:

https://github.com/smalos/nuBuilder4-Bu ... e_types.md

Basically, it checks whether the column is of type date and if the field value is empty, null is written to the DB.

Re: Deleting a date from a record

Posted: Wed Jun 10, 2020 8:16 am
by kev1n
Please let me know if it works for you.

Re: Deleting a date from a record

Posted: Thu Jul 09, 2020 11:44 pm
by admin
kev1n,

I have added your fix.

Thanks


Steven