Skip to content

Nullable field can not be overwritten by null  #2196

Open
@sFreezer

Description

@sFreezer

Bug Report

Q A
BC Break no
Version since 2.1.1

Summary

After this -> change

you can not overwrite any value on a nullable field to null with upsert.
because it is just set on $setOnInsert and not on $set.
I think: this was done to not overwrite a value by accident (by not calling the setter)

but we need to overwrite a value by setting it to null on purpose

Current behavior

class SomeDocument
{
    /**
     * @ODM\Id(strategy="NONE", type="integer")
     */
    private int $id;

    /**
     * @ODM\Field(type="integer", nullable=true)
     */
    private ?int $someNullabeInt = null;

    public function getId(): int
    {
        return $this->id;
    }

    public function setId(int $id): void
    {
        $this->id = $id;
    }

    public function getSomeNullabeInt(): ?int
    {
        return $this->someNullabeInt;
    }

    public function setSomeNullabeInt(?int $someNullabeInt): void
    {
        $this->someNullabeInt = $someNullabeInt;
    }
}
    public function test_Nullable(): void
    {
        $id = 1;

        $document = new SomeDocument();
        $document->setId($id);
        $document->setSomeNullabeInt(2);

        $this->documentManager->persist($document);
        $this->documentManager->flush();
        $this->documentManager->clear();

        $documentUpdate = new ProductMetricDocument();
        $documentUpdate->setId($id);
        $documentUpdate->setSomeNullabeInt(null);

        $this->documentManager->persist($documentUpdate);
        $this->documentManager->flush();
        $this->documentManager->clear();

        $repository = $this->documentManager->getRepository(SomeDocument::class);
        $fetchedDocumentAfterUpdate = $repository->find($id);
      }

$someNullabeInt will be 2 after running this code but it should be null

How to reproduce

by running the test from #Current behavior

Expected behavior

we need to be able to overwrite a value with null on purpose by calling the setter on null

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