Re: DB_DataObject - update only if changed
| From: | Geoff Hopson | Date: | Wed, 05 Mar 2003 14:18:20 +0000 |
| Subject: | Re: DB_DataObject - update only if changed | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-4045@lists.php.net to get a copy of this message | ||
Hi,
I have written a method called 'updateFrom', which is basically a merger between setFrom and updateFrom - I use the 'dirty' flag in the setFrom part and check the flag in the update part. Seems to work OK with my limited testing so far
If anyone wants it, let me know - it's currently sitting in my subclass of DataObject so I don't corrupt the PEAR package.
Thanks for the response,
Geoff
On Wednesday, March 5, 2003, at 02:05 pm, Alan Knowles wrote:
Geoff Hopson wrote:Geoff Hopson Objectology Ltd. http://www.objectology.co.uk/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: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- 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)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/