sokil / php-mongo

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

Add support set in fields stdClass in Structure::prepareToStore #173

Closed MCubs closed 5 years ago

MCubs commented 5 years ago

Hello,

I created a patch that adds the ability to save an empty object to the collection. This need arose due to the nature of my system. The save method in the class Document saves an empty object as an array. Playback example: I have Model Car extended Document. Create $car = new Car(); $car->set('human-id', 1)->save(); $car->update(["human-id" => 1], ['$set' =>["markets-data.1" => ["car" => 1]]]); Data in DB:

{
    "_id" : ObjectId(""),
    "markets-data" : {
        "1" : {"car": 1}
    },
    "human-id" : 1
}

$car->update(["human-id" => 1], ['$unset' =>["markets-data.1" => ["car" => 1]]]); Data in DB:

{
    "_id" : ObjectId(""),
    "markets-data" : {},
    "human-id" : 1
}

$car->save(); Data in DB:

{
    "_id" : ObjectId(""),
    "markets-data" : array(),
    "human-id" : 1
}

Here is an example of how update works in the native view:

> db.vehicle.update({"human-id": 36}, {'markets-data': {}, "human-id": 36});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.vehicle.find({"human-id": 36}, {'markets-data': 1, 'human-id': 1}).pretty();
{
    "_id" : ObjectId(""),
    "markets-data" : {},
    "human-id" : 36
}

How to use my patch: Create $car = new Car(); $car->set('human-id', 1)->save() Data in DB:

{
    "_id" : ObjectId(""),
    "human-id" : 1
}

$car->set('markets-data', new \stdClass)->save(); { "_id" : ObjectId(""), "markets-data" : {}, "human-id" : 1 }

MCubs commented 5 years ago

When i can get instruction for run docker?

sokil commented 5 years ago

When? Now! https://github.com/sokil/php-mongo#docker-phpunit-tests )