Re: Re: DB_DataObjects save method
| From: | Hans L | Date: | Fri, 29 Oct 2004 21:13:53 +0000 |
| Subject: | Re: Re: DB_DataObjects save method | ||
| References: | 1 2 3 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-34167@lists.php.net to get a copy of this message | ||
Hi Mike,
Mika Tuupola wrote:
On Fri, 29 Oct 2004, Hans L wrote:Yeah, that's pretty easy to handle. When you save() you reset your $modifiedColumns array and $isNew, if applicable.To illustrate, with typical mutator method: function setName($v) { if ($v !== $this->name) {$this->modifiedColumns[] = "name"; $this->name = $v;} } That way when you actually perform the UPDATE, you can quickly build the column list from the $modifiedColumns array. Plus this give you an easy way to check whether objects are modified: $obj->isModified() or $obj->getModifiedColumns().Only works if the object was originally loaded from the database. What about if you created the object from for example $_POST array and later modified it. Would you then do an UPDATE?
IMO determining from primary key is quite simple and
convenient method to find out whether you should
do an UPDATE or INSERT.
Yeah, that works fine, but Aurthur's post I was responding to was a suggestion that only modified columns get updated. The use of pkey to determine update/insert doesn't address that need.
Yeah -- optimistic locking would work, but would still require doing a SELECT, but it's a pretty good compromise. Again, though, this doesn't address Aurthur's suggestion, since this wouldn't indicate which cols had changed. -HansThis is of course if you have a primary key, but most of the time you do anyway.Of course this doesn't account for possibility that someone else changed record in the background, but PHP in general is going to suck at those scenarious without an SRM solution and lots of hard-to-debug code to debug persistence code. Doing a SELECT before the update would be possible, but would add a lot of overhead. Plus this approach wouldWhy not just store a timestamp and compare that before saving?>