akeeba / fof

Rapid Application Development framework for Joomla!™ 3 and 4
0 stars 0 forks source link

How do you treat integer columns with default value 0 #518

Closed compojoom closed 9 years ago

compojoom commented 9 years ago

I've been running into the following problem. Whenever I'm dealing with an integer field that I haven't explicitly declared a default value for or I have set a default value of 0, the automatic check in DataModel tells me that my field is empty.

The only way I can go around the automatic check is to set the default value of the field to NULL, but I somehow don't want to do this since the field type is INT.

The other option I see is using fieldsSkipChecks variable, but that requires typing the field name and I thought FOF was all about magic :)

What are you guys doing in such situations?

nikosdion commented 9 years ago

What happens makes sense. You don't have a default value so FOF can't understand what you mean with "validate this field for me automatically". Therefore you either need to give it a default value OR tell FOF to not validate it.

The architecturally correct solution is declaring a default value. If you have an INT field a NULL value should not be an acceptable choice. Given how PHP's empty() works it'd make much sense to have a zero value. It'll also be better for indexing, in case this column belongs to an index.

compojoom commented 9 years ago

I have nothing against setting a default value of 0. Actually that is what I do I still get an error for the field: [code] if (($field->Null == 'NO') && empty($value) && !is_numeric($value) && !in_array($fieldName, $this->fieldsSkipChecks)) { if ($field->Default) { $this->$fieldName = $field->Default;

                continue;
            }

[/code]

since $field->Default is 0, we never go into this check.

So the only option I'm left with is adding it to the $this->filedsSkipChecks, or making the if($field->Default) a little more complicating by checking for the fieldType and then accounting for a default value of 0?

Eighke commented 9 years ago

Does it work with a VARCHAR and 0 as default? I guess but PHP will probably consider it is empty. Same for ENUM or any other field.

nikosdion commented 9 years ago

Ah, I see what you mean. IIRC this had to do with how PHP handled NULL default values. Can you please change the line if ($field->Default) to if (!is_null($field->Default)) and let me know if that fixes your issue? It was on my to-do list but I never got the time to actually test it. In short: Daniel, can you please be my guinea pig?

compojoom commented 9 years ago

Yes, this works now. My field default value is 0 and I'm able to save when the line is: if (!is_null($field->Default))