[TYPO3-mvc] Extbase: No way to store NULL / leave form fields empty?

g4-lisz at tonarchiv.ch g4-lisz at tonarchiv.ch
Wed Apr 22 21:26:10 CEST 2015


Hi Philipp

On 04/20/2015 07:09 PM, Philipp Wrann wrote:
> Its more a hard-to-achieve thing in an ORM i think. If a models
> property is defined as integer, of what use would it be to store null
> in the database?
To store that a certain field is not set. Why can you save empty
strings, but emtpy numeric fields you can't?

>
> If you realy need the field to be null try to use a Hook, the
> persistenceManager emits signals before and after it updates objects,
> you could simply get the objects values in the afterPersistChanges
> Signal and if it is 0 or -1 you set it to null.
Numeric values shouldn't be translated, only empty fields...

So that's what I did in the end:
The POST values from the form are handled as strings first. If the
string is empty, then it's NULL. If not, it's converted to double:

    /**
     * Sets the mw
     *
     * @param mixed $mw
     * @return void
     */
    public function setMw($mw) {
        if($mw === '')
            $this->mw = null;
        else
            $this->mw = floatval($mw);
    }

It's not pretty, but it works.

>
> Anyway... If a property is configured to be an integer it is the
> common behaviour that it will be mapped as integer, so the code is
> more robust.
>
> I would go for a different approach. You could create those values as
> 1:N Relation, so only relations WITH a value would be persisted. You
> base model would be smaller and you would not need so many keys if you
> want to filter on those fields.
This works in theory, but it makes the use of form viewhelpers almost
impossible. And it makes DB requests slow because of lots of joining and
grouping.

Cheers,
Till




More information about the TYPO3-project-typo3v4mvc mailing list