coen-hyde / Shanty-Mongo

Shanty Mongo is a mongodb library for the Zend Framework. Its intention is to make working with mongodb documents as natural and as simple as possible. In particular allowing embedded documents to also have custom document classes.
Other
200 stars 52 forks source link

Issue updating document set of existing doc #40

Closed tholder closed 13 years ago

tholder commented 13 years ago

Trying to create a document with a document set.

There is a unique key on the email field though, if a dupe is detected whilst saving, I want to take the contents of the documentset and save it against the existing row.

Originally I did just:

//Save it to existing document. $i = Model_Mongo_Invite::one(array('email' => $this->email)); $i->companies - $this->companies; $i->save();

That didn't work, it created two documents like:

{ "_id" : ObjectId("4e88794093fd9afc1c000000"), "_type" : [ "Model_Mongo_Invite" ], "companies" : [ { "company" : { "$ref" : "company", "$id" : ObjectId("4e88607993fd9ab31a070000") }, "addedByUser" : { "$ref" : "user", "$id" : ObjectId("4e88607993fd9ab31a060000") }, "createdAt" : 1317566784 } ], "email" : "tom@simpleweb.co.uk", "secret" : "MTQxZWQ0MWViNGIwYzY4MzQ0MGU3NzQxYmJjNjNjNjU" } { "_id" : ObjectId("4e88794293fd9afc1c010000"), "companies" : [ { "company" : { "$ref" : "company", "$id" : ObjectId("4e88607993fd9ab31a070000") }, "addedByUser" : { "$ref" : "user", "$id" : ObjectId("4e88607993fd9ab31a060000") }, "createdAt" : 1317566786 } ] }

I assumed this didn't work at first because the old documentset was still referencing the other object, so I converted to the following but got the same error:

See https://gist.github.com/1257513 for code of the classes.

tholder commented 13 years ago

I'm closing this issue, something to do with the order in which I'm saving documents.

I would say though working with documentsets is proving to be tricky, I will try and write some documentation when I've got my head round it. Seems like order in which things are saved is very important.

coen-hyde commented 13 years ago

Hey Tom,

Yeah you weren't using DocumentSets as intended. Checkout https://gist.github.com/1258108. When using the documentset->new() method it creates a child document but does not yet attach it to the parent document set. Only when save() is called on the child document is it pushed into the document set on the db end. This is a bit confusing but it was done like this to avoid race conditions.

Cheers, Coen