EasyAdminBundle icon indicating copy to clipboard operation
EasyAdminBundle copied to clipboard

If I store "1000" in a NumberField with decimals, doctrine thinks it changed from "1000.00" to "1000" every time I flush the entity

Open nevez opened this issue 1 year ago • 0 comments

EasyAdmin 4.9.4.

I have a field in an entity defined like this:

#[ORM\Column(type: Types::DECIMAL, precision: 10, scale: 2, nullable: true)]
private ?string $importoAs = null;

in its CRUD Controller it's defined like this:

 NumberField::new('importoAs')->setNumDecimals(2)->setNumberFormat('%.2f'),

If I enter an integer number (no decimals) like 1000 in this field and persist the entity in the database, every entity update following it has the field "importoAs" included in its changeset, forcing an UPDATE SQL statement to be issued even thou the field hasn't been touched.

It seems like the problem resides in the way these numbers are handled as strings: since 1000 is different from 1000.00, the update is issued every time.

This is confirmed by the fact that this sprintf in the setImportoAs() entity method fixes the problem:

    public function setImportoAs(?string $importoAs): static
    {
        $this->importoAs = sprintf('%.2f', $importoAs);

        return $this;
    }

If it's not an easy fix, you might want to mention this workaround in the documentation.

nevez avatar Sep 04 '24 09:09 nevez