Discussion:
[Modeling-users] willChange - under the hood
Tom Hallman
2004-02-20 13:25:04 UTC
Permalink
Greetings!

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.

One question I have is on what "willChange" actually does. As I
understand 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, only "dirty" attributes get updated. Is this accurate?

Thanks in advance for any insight.

~Tom

___________________________________________________________
Tom Hallman DiscipleMakers, Inc
***@dm.org http://www.dm.org/~hallmant
531 Brittany Dr * State College * PA * 16803 * 814-861-4591
Go therefore and make disciples of all nations (Matt 28:19)
Sebastien Bigaret
2004-02-20 17:48:13 UTC
Permalink
Hi Tom,
Post by Tom Hallman
Greetings!
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.
One question I have is on what "willChange" actually does. As I understand
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, only
"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
Post by Tom Hallman
from AuthorBooks import Book
from Modeling.EditingContext import EditingContext
ec=EditingContext()
b=ec.fetch('Book')[0] # get whatever book
b
<AuthorBooks.Book.Book object at 0x85060fc>
Post by Tom Hallman
b.willChange()
ec.allUpdatedObjects()
[<AuthorBooks.Book.Book object at 0x85060fc>]
Post by Tom Hallman
ec.saveChanges() # does not send any sql statement to the db
... fetched_title=b.getTitle()
Post by Tom Hallman
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>]
Post by Tom Hallman
ec.saveChanges() # does not send any sql statement to the db either
Wishing you a nice trip with the framework!)

-- Sébastien.

Loading...