sokil / php-mongo

MongoDB ODM. Part of @PHPMongoKit
http://phpmongokit.github.io/
MIT License
242 stars 46 forks source link

support empty plain object as value #143

Closed svsool closed 7 years ago

svsool commented 7 years ago

more information you can find here https://jira.mongodb.org/browse/PHP-172

sometimes you need save empty plain object {} as value

$document->set('myValue', new stdClass);

result:

{
  myValue: {}
}

but now it always saved as ↓, because value casts to array

{
  myValue: []
}
coveralls commented 7 years ago

Coverage Status

Coverage decreased (-0.03%) to 91.925% when pulling 11e8d02674393380f9e52433ce03a4e2952523d3 on svsool:master into d42e2bca3d716e2c80fc9389b13faed41d8d3970 on sokil:master.

svsool commented 7 years ago

looks like I need fix some tests

sokil commented 7 years ago

Tests are not the main problem. There are few other: 1) Back compatibility, when code starts to save {} but on modify expects [] 2) Hydrating new document object with {} in field. With your code, if you save document first time, you'v got {} in db, then you read document and this field became [], because driver do not distinguish [] and {} on read from db.

So this must be done by specifying type of field, or something like that. Then hydrated and repeatedly saved document also will have {}.

By the way, why do you need {} in db, you may omit saving this field, and modilying empty field must be correctly handled.

svsool commented 7 years ago

Some legacy code may require this, but I agree with you anyway

sokil commented 7 years ago

I.l add field type functionality snd inform you here https://github.com/sokil/php-mongo/issues/144

coveralls commented 7 years ago

Coverage Status

Coverage decreased (-0.03%) to 91.925% when pulling 11e8d02674393380f9e52433ce03a4e2952523d3 on svsool:master into d42e2bca3d716e2c80fc9389b13faed41d8d3970 on sokil:master.