Re: Classic 2.6.4 eval - Error when insert none required field
Reply #37 –
So, here's a workaround that works. Instead of removing the field from the $modeldata array in the BeforeAdd and BeforeUpdate events, set it to NULL:
if ( empty( $modeldata['Teams_teamId'] ) ) $modeldata['Teams_teamId'] = null;
PDO is smart enough to know the difference between an empty string ('') and a null string and generates the correct database insert/update when the value is null.
This is still a workaround and hopefully will not be accepted as a solution. This creates too much extra/unnecessary work for the developer. IMO, since PHPRad already knows that the field is NULLable and that it is not required, this should be the default action taken by PHPRad for any NULLable column.
Here's what I would like to see PHPRad do:
- When retrieving information from the database catalog:
- Set the field's default value to the database default for the field
- If there is no default value in the catalog and the field is NULLable, set the field's default value to NULL
- If the field is NULLable, set the field's default field-required to false
- Provide a NULL default option (checkbox) on the Manage Database dialog that, when checked, would cause the default value to be set to NULL (not the literal 'NULL'). This cannot be a literal in the DefaultValue field because it would not allow for the literal 'NULL' to be used as a default value.
- Modify the controller to set the value to NULL when the field has the NULL default option checked and the field is empty