rantanplan
rantanplan

Reputation: 422

TYPO3 Extbase: How to store NULL in DB for empty numeric fields?

In a form Viewhelper I have lots of numeric fields, which should be empty by default, not filled with '0' or '0.00'.

I need this, because there should be a different handling of the data in the saveAction, depending on left empty fields or filled in values (including 0).

For this reason I set the field properties in the database table to NULL by default, like:

CREATE TABLE IF NOT EXISTS `tx_myext_domain_model_result` (
    mw double (10,2) DEFAULT NULL,
    mw2 double (10,2) DEFAULT NULL,
    ....
);

TCA looks like this:

  'mw' => array(
    ...
        'config' => array(
            'type' => 'input',
            'size' => 30,
            'eval' => 'double2,null'
        )
    ),

Also, in the Result model class, the corresponding properties are initialized with NULL;

/**
 * mw
 *
 * @var float
 */
protected $mw = null;

I did this check to assure that NULL is handled over to the getter for empty form fields:

public function setMw($mw) {
        if ($mw == NULL)
            $this->mw = 999;
        else
            $this->mw = $mw;
}

This did what I excepted: The mw field DB was set to '999' for a empty form field.

But now, switched back to normal setter,

public function setMw($mw) {
       $this->mw = $mw;
}

fields in the DB table are only left NULL when all other form fields are left empty, too. I.e. as soon as I enter a value in one of the form fields, all other empty form fields are set to '0' on save.

Adding null to the TCA eval field didn't do the trick, neither.

How can I change this behaviour? I'm using TYPO3 6.2.x

Upvotes: 0

Views: 1007

Answers (1)

Fixus
Fixus

Reputation: 4641

step 1: in initializeAction set null for the property step 2: the idea with checking $mw is null is preaty good idea. TYPO3 not always do what you can assume using logical thinking :) So check if(is_null($mw)) $this->mw = null

this two things should do the trick

you can also set a default value in setter param

setMw($mw = null)

Upvotes: 0

Related Questions