Re: DB_DataObject - update only if changed

From: Date: Wed, 05 Mar 2003 14:05:13 +0000
Subject: Re: DB_DataObject - update only if changed
References: 1  Groups: php.pear.general php.pear.general 
Request: Send a blank email to pear-general+get-4044@lists.php.net to get a copy of this message
Geoff Hopson wrote:
Hi, I'm playing around with the DB_DataObject stuff (very nice - thanks!), but have a requirement only to update columns that have been changed. Right now the update method seems to set all columns to the current attributes of the object. For example, I have a user object with username, password, name attributes. If I change only the name on a form, the update statement is update users set username='blah', password = 'blah', name = 'newblah' where usr_id = 6; I would like the statement to be: update users set name = 'newblah' where usr_id = 6; Basically, I would like something like setFrom to only set the attributes that have changed and to mark them as dirty somehow. First question: are there plans to do this Now on to my planned solution - all comments appreciated! In setFrom:
    - test the new value against the old value
    - if different, set the old value to the new value AND
        - set the $items so that the value for the attribute (eg DB_DATAOBJECT_STR) is bitwise-ANDed with a new value DB_DATABASE_DIRTY (which I have set to be 128)
I'd be tempted to add another method rather than overloading setFrom too much.. $object->get($someid); $copy = $object->__clone(); $object->setFrom($object); $object->unsetIdentical($copy); // new method??? $object->update(); if you simple unset the identical values (and ignore the keys) it shouldnt build the update with only the changed values... Regards Alan
in update():
    - rejig the query builder to test for the DB_DATABASE_DIRTY bit, and if the attribute is dirty, use it in the query
I'm playing with this right now - I can post the code if and when I get it working, but just wanted to ensure that I wasn't doing something stoopid (PHP novice...I'm allowed some mistakes :-) Thanks for any input, Geoff -- Geoff Hopson Objectology Ltd. http://www.objectology.co.uk/


« previous php.pear.general (#4044) next »