2

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

2 Answers2

3

Decimal is not supported for mongodb-odm yet (Oct 12 2019) v2.0.1 (see Supported Types).

It was introduced in mongodb version 3.4 only as stated in the BSON Types mongoDB docs, so, it need to be added to mongodb-odm.

You can fork the project and add the type yourself and also make a pull request.

I would copy the float type, create a decimal type, change its fetch and write logic to handle PHP - MongoDB\BSON\Decimal128 and finally update the Type.php to include the new DecimalType.

filipe
  • 1,957
  • 1
  • 10
  • 23
  • FWIW I've opened an issue (https://github.com/doctrine/mongodb-odm/issues/2086) to track this feature request. We'll gladly accept a PR! – malarzm Oct 15 '19 at 16:22
  • They committed decimal128 to master 3 days ago. If you're feeling adventurous you can test it by using the master branch with `composer require doctrine/mongodb-odm:dev-master` The actual release for this feature will be 2.1 which is most likely still a couple of months away. – Stefan May 09 '20 at 21:52
-1

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