简体   繁体   English

TYPO3 Extbase:如何在DB中为空的数字字段存储NULL?

[英]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'. 在Viewhelper表单中,我有很多数字字段,默认情况下应为空,而不用'0'或'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). 我需要这样做,因为saveAction中的数据应该有不同的处理方式,具体取决于左侧的空字段或填充的值(包括0)。

For this reason I set the field properties in the database table to NULL by default, like: 因此,我默认将数据库表中的字段属性设置为NULL,例如:

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: TCA看起来像这样:

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

Also, in the Result model class, the corresponding properties are initialized with NULL; 同样,在Result模型类中,相应的属性用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: 我进行了此检查,以确保将NULL处理为空表单字段的getter:

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. 这样做的确是我所例外的:空字段字段的mw字段DB设置为“ 999”。

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. 当所有其他表单字段也都留空时,仅将DB表中的字段保留为NULL。 Ie as soon as I enter a value in one of the form fields, all other empty form fields are set to '0' on save. 即,只要在表单字段之一中输入值,保存时所有其他空表单字段就会设置为“ 0”。

Adding null to the TCA eval field didn't do the trick, neither. 向TCA eval字段添加null并不能解决问题。

How can I change this behaviour? 我该如何改变这种行为? I'm using TYPO3 6.2.x 我正在使用TYPO3 6.2.x

step 1: in initializeAction set null for the property step 2: the idea with checking $mw is null is preaty good idea. 步骤1:在initializeAction中将属性设置为null步骤2:检查$ mw为null的想法是一个好主意。 TYPO3 not always do what you can assume using logical thinking :) So check if(is_null($mw)) $this->mw = null TYPO3并不总是使用逻辑思维来做您可以假设的事情:)所以检查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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM