Skip to content

Empty string will be saved as "0" in database #13

@richardhj

Description

@richardhj

Checklist before I submit this issue report

I confirm that:

My environment is:

(Please fill in the actual values from your environment)

Key Value Comments
PHP version: 7.0
Contao version: 3.5.21
MetaModels version: hotfix15
Installation via composer yes
Installed MetaModels packages all

Issue description

When the attribute value is an empty string (''), it will be saved as "0" in the database. Expected behavior would be saving null.

Steps to reproduce

/** @var \MetaModels\IItem $item */
$item->set('decimal-attr', '');
$item->save();

This could be added to the attribute class.

    public function setDataFor($arrValues)
    {
        // Translate all '' values to null, so they will not set as `0` in the database.
        $arrValues = array_map(function ($value) {
            return '' === $value ? null : $value;
        }, $arrValues);

        parent::setDataFor($arrValues);
    }

I'm not sure, whether this is a issue, because this does not happen when using the dcg (in the back end).
When this issue will be confirmed, I could provide a PR.

Same for attribute_numeric.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions