Feature/save only changes #41

Merged
merged 3 commits into from Aug 16, 2012

Projects

None yet

2 participants

@chrsm
Contributor
chrsm commented Aug 11, 2012

Hello, good tidings, and all that fun stuff!

I apologize in advance for borking the commits (merge into master from ... yadda), I tried to squash them into one and apparently I am not more of a git than git expects. But I digress.

Prior to now, Object::save() called Storage::save(), which ended up calling insert() no matter what.

I added functionality to update, as well as changed the Object::__getData method to accept a param that only returns 'new' or 'dirty' data.

When Storage::update() is called, it gets all the new or changed data, and sends it off to MongoDB using $set.

The unfortunate side effect of this is that embedded documents do not use $push or $pull. I will be working on that in the future.

chrsm added some commits Aug 11, 2012
chrsm [FEATURE] $set - Saving only changes.
This needs a little bit of work - Embedded data will update the
whole property and not just $push or $pull - I will work something
out for this.

Changes:
	Object::__getData - new param, $whereNew. Tells __getData
	to only return properties that have changed or are new.
	Unfortunately, for collections (composeMany, etc) anything
	added will cause the whole collection to be updated
	as well, which is something I need to work on.

	Storage::update - Not just an alias for insert() now.
	Gets all changed or new data from an object and sends it to
	MongoDB over $set.
2e24a7b
chrsm [FEATURE] Save only changes on update using 34ac11d
chrsm Merge branches 'master' and 'feature/save-only-changes' 18cf4e2
@a-musing-moose
Owner

Hi Chris,

Thanks for the pull request. I am not sure if these changes are necessary. The save function in storage kinda does it already. It checks the status and then either inserts, update, or if the status is clean it does nothing.

What this feature really is asking for is the ability to only update those properties of an object that have changed since it was retrieved form the db.

e.g.

start with: {_id: 123, 'a': 'a', 'b': 'b'}
end with: {_id: 123, 'a': 'c', 'b': 'b'}

This would eventually product a update such as:

{ $set : { 'a' : 'c' } }

i.e. only the fields which have changed get pushed.

I think this means that we would need to move the status to a per-property thing and then the _getData() function would have a flag similar to what you have done.

e.g.

public function _getData($changedOnly=false)

This should return an associate array containing only those properties which have changed.

Thanks again for your interesting in Morph,
Jon

@chrsm
Contributor
chrsm commented Aug 14, 2012

That's actually exactly what this does. It only saves the fields that have been changed after the retrieval of an object. It only uses $set if an object has an id (i.e., has been saved).

The status is on a per-property basis, as each property is checked for whether or not it is dirty or new. Perhaps I'm missing something here, or I messed up the commits a bit too much trying to squash them.

@a-musing-moose
Owner

Hey Chris,

Damn - I must have missed that when I was running through the pull
request - clearly i'm an idiot :-). Sorry mate. Re-open the pull request
and I'll merge.

Regards,
Jon

That's actually exactly what this does. It only saves the fields that
have been changed after the retrieval of an object. It only uses $set
if an object has an id (i.e., has been saved).

The status is on a per-property basis, as each property is checked for
whether or not it is dirty or new. Perhaps I'm missing something here,
or I messed up the commits a bit too much trying to squash them.


Reply to this email directly or view it on GitHub
#41 (comment).

Jonathan Moss
Director

Tangent Snowball
Suite 109
175 Sturt Street
Melbourne, VIC 3006
ABN: 76 137 883 615

T: +61 (0) 3 9645 8796
E: jonathan.moss@tangentsnowball.com.au
W: www.tangentsnowball.com.au

@chrsm
Contributor
chrsm commented Aug 14, 2012

No problemo at all.

Unfortunately, I cannot re-open this pull request, but I can submit another if you'd like. Ideally, I'd figure out how to squash these commits properly, but apparently git does not like me.

@a-musing-moose
Owner

Huzzah! Apparently I can reopen it. So thanks for the pull request and for correcting me :)

Cheers Chris.

@a-musing-moose a-musing-moose merged commit f464e83 into a-musing-moose:master Aug 16, 2012
@chrsm chrsm deleted the chrsm:feature/save-only-changes branch Jan 16, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment