Skip to content
This repository has been archived by the owner on Jun 11, 2021. It is now read-only.

DocumentSet corrupted after saving #111

Open
michael-mader opened this issue Jan 25, 2013 · 1 comment
Open

DocumentSet corrupted after saving #111

michael-mader opened this issue Jan 25, 2013 · 1 comment

Comments

@michael-mader
Copy link

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

@coen-hyde
Copy link
Owner

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants