Son Vu Fri Jan 06 23:51:48 -0500 2012

Subject: is_dirty()

It appears that is_dirty() returns true even when I set an attribute of a model with the same exact value as the original.

For example:
$uinfo = User::find_by_id(1)
// ============================//
ORIGNAL DATA FOR $uinfo:
first_name = 'Son';
last_name = 'Vu';
phone = '123';
website = 'www.vulogic.com'
// ============================//

Setting the attribute "phone" to a value of "123" should not cause the object to be dirty because the data is in essence the same as the original. Since the attribute is marked as dirty, this causes an UPDATE statement to be created when "$uinfo->save()" is called. As a result, you end up running a redundant query.

To ensure that an attribute is genuinely "dirty", I added the lines of code below to "Model.php" at line 451.

// Make sure that attributes are truly "dirty"

if(isset($this->attributes[$name])){

if($this->attributes[$name] != $value){
$this->attributes[$name] = $value;
$this->flag_dirty($name);
}

} else {

$this->attributes[$name] = $value;
$this->flag_dirty($name);

}


Bill Ortell Fri Jan 13 14:53:22 -0500 2012

I had to do something very similar some time back... frustrating....

Mr. Carl Wed Jan 25 00:07:38 -0500 2012

There reason be hide this. Take look at Comparison Operators

Reason: $a != $b so TRUE if $a is not equal to $b AFTER TYPE juggling. <=== Got Love it!

1 var_dump(0 == "a"); // 0  == 0 -> true
2 var_dump("1" == "01"); // 1  == 1 -> true
3 var_dump("10"  == "1e1"); // 10 == 10 -> true
4 var_dump(100 == "1e2"); // 100 == 100 -> true
5 
6 // Need use: $a ! === $b     Not identical 
7 // TRUE if $a is not equal to $b, or they are not of the same type.

The power be hide php is the ability to type juggling.
Luke Stokes Sat Feb 25 15:58:32 -0500 2012

I was actually going to post about the same thing. Here's what I did:
[code]
Index: Model.php ===================================================================
--- Model.php (revision 3648)
+++ Model.php (working copy)
@ -449,8 +449,13 @
if ($value instanceof DateTime)
$value->attribute_of($this,$name);

+ // ADDED BY FoxyCart
+ $flag_as_dirty = (isset($this->attributes[$name]) && $this->attributes[$name] === $value) ? false : true;
+
$this->attributes[$name] = $value;
- $this->flag_dirty($name);
+ // ADDED BY FoxyCart
+ if ($flag_as_dirty)
+ $this->flag_dirty($name);
return $value;
}
[/code]

(1-3/3)