Shanty-Mongo icon indicating copy to clipboard operation
Shanty-Mongo copied to clipboard

DocumentSet corrupted after saving

Open michael-mader opened this issue 13 years ago • 1 comments

There is a big issue with calling save() on a DocumentSet (probably it is a problem with all embeded Documents).

Consider following model:

class Model_User extends Shanty_Mongo_Document{

    protected static $_db = "test";
    protected static $_collection = "users";

    protected static $_requirements = array(
        'name' => 'Required',
        'addresses' => 'DocumentSet',
        'addresses.$' => 'Document',
        'addresses.$.street' => 'Required'
    );  
}

Now if I create a new user or get a existing user from MongoDB and change something inside the DocumentSet and call save() directly on the DocumentSet every thing get saved. BUT after saving the DocumentSet (or the Document as DocumentSet does not overwrite save()) it recieves the new values directly from MongoDB and sets the _cleanData field with these values. But MongoDB returns the complete document and not only the changed part of the document, so _cleanData in the DocumentSet contains the complete Document.

See below example:

$user = new Model_User();
$user->name = "Peter";
$adr = $user->addresses->new();
$adr->street = "Milkyway";
$user->addresses[] = $adr;
$user->save();

$user->addresses[0]->street = "Marsstreet";
$user->addresses->save();

print_r($user->addresses);

$user->addresses->_cleanData before save:

[_cleanData:protected] => Array
        (
            [0] => Array
                (
                    [street] => Milkyway
                )

        )

$user->addresses->_cleanData after save:

[_cleanData:protected] => Array
        (
            [_id] => MongoId Object
                (
                    [$id] => 5102878433b5f9141d000002
                )

            [_type] => Array
                (
                    [0] => Model_User
                )

            [addresses] => Array
                (
                    [0] => Array
                        (
                            [street] => Marsstreet
                        )

                )

            [name] => Peter
        )

This makes working with the saved DocumentSet impossible.

The save method needs to check which part of the document is saved and retrieve the right part of the document to write to _cleanData

There are two workarounds for this problem at this time:

  1. call save on root document
  2. read document from DB after calling save on DocumentSet

I really hope you can fix this soon.

Thanks in advance!

Best regards, Fender

michael-mader avatar Jan 25 '13 13:01 michael-mader

Wow that's a big issue. I'll look at this as soon as i can.

coen-hyde avatar Jan 27 '13 09:01 coen-hyde