Re: [Modeling-users] willChange - under the hood
Status: Abandoned
Brought to you by:
sbigaret
From: Sebastien B. <sbi...@us...> - 2004-02-20 19:47:19
|
Hi Tom, Tom Hallman <hal...@dm...> wrote: > Greetings! >=20 > I am just about to start doing some work with Modeling, and I'm very > excited about its capabilities! It looks very promising, to say the least. >=20 > One question I have is on what "willChange" actually does. As I unders= tand > it, Modeling captures a single "snapshot" of some fetched rows (objects),= and > whenever "willChange" is called on a particular object's attribute, that > attribute is made "dirty", such that when "ec.saveChanges()" is called, o= nly > "dirty" attributes get updated. Is this accurate? Absolutely, except one detail: willChange() marks the whole object as updated (not a particular object's property); the object is then examined when the EC saves changes. The snapshots are registered in the central Database object, and when the EC saves changes, the objects that were marked as updated (the "dirty" ones) are compared to their snapshots and the observed changes are propagated to the database. Hence, a "dirty" object that in fact did not change will not cause any SQL UPDATE to be sent to the database --you can verify that by setting MDL_ENABLE_DATABASE_LOGGING to "yes" and observing the sql statements (not) sent to the database. For example, using the AuthorBooks test model: >>> from AuthorBooks import Book >>> from Modeling.EditingContext import EditingContext >>> ec=3DEditingContext() >>> b=3Dec.fetch('Book')[0] # get whatever book >>> b <AuthorBooks.Book.Book object at 0x85060fc> >>> b.willChange() >>> ec.allUpdatedObjects() [<AuthorBooks.Book.Book object at 0x85060fc>] >>> ec.saveChanges() # does not send any sql statement to the db >>> # same if you modify an obj. property then revert to the original value: ... fetched_title=3Db.getTitle() >>> b.setTitle("this is a new title, not the fetched one") >>> b.setTitle(fetched_title) # restore the original value >>> ec.allUpdatedObjects() [<AuthorBooks.Book.Book object at 0x85060fc>] >>> ec.saveChanges() # does not send any sql statement to the db either Wishing you a nice trip with the framework!) -- S=E9bastien. |