mongodbphp-7.2doctrine-odmmezzio

Doctrine ODM - float field not working with decimal field


I have a document in doctrine with the field config set to

/** @ODM\Field(type="float") */
public $stock_price;

and on my mongodb the field has a validation for decimal

  stock_price: {
    bsonType: 'decimal',
    description: 'must be a decimal and is required'
  },

When I try to store the values it return document validation failed or cannot convert decimal to float

$odm = new stock();
$odm->stock_price = 10.99;
$dm->persist($odm);
$dm->flush();

return document validation failed

If I try to convert to mongo decimal, I got failed insert too.

$odm = new stock();
$odm->stock_price = new Decimal128("10.99");
$dm->persist($odm);
$dm->flush();

return cannot convert decimal to float


Solution

  • Thanks @filipe for your explanation, but the solution I found for me, was change the validation to double, and all work fine.