Skip to content

Document HydratorFactory does not support nullable date field #2753

Open
@frenchcomp

Description

@frenchcomp

Bug Report

The HydratorFactory does not properly support nullable date fields. So typed properties are not initialized and there an issue with PHP because typed property must not be accessed before initialization.

Q A
BC Break no
Version 2.11

Summary

In a my Document Class, I have a nullable date field :

protected ?DateTimeInterface $moderatedAt = null;

Its mapping (in XML) is :

<field field-name="moderatedAt" name="moderated_at" type="date" nullable="true"/>

The generated hydrator code for this field is :

       // Field(type: "date")
        if (isset($data['moderated_at'])) {
            $value = $data['moderated_at'];
            if ($value === null) { $return = null; } else { $return = \Doctrine\ODM\MongoDB\Types\DateType::getDateTime($value); }
            $this->class->reflFields['moderatedAt']->setValue($document, clone $return);
            $hydratedData['moderatedAt'] = $return;
        }

Current behavior

But, $data['moderated_at'] is present and at null. So if (isset($data['moderated_at']))
will be never true. And the next nested condition will be never executed

Expected behavior

The generated code must be like this :

if (isset($data['moderated_at']) || (! empty($this->class->fieldMappings['moderatedAt']['nullable']) && array_key_exists('moderated_at', $data))) {

Thanks :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions