Home » Database » DB_DataObject » Bug #5881
Put parens around whereAdd's if 'or' is present
Details
| Request #5881 | Put parens around whereAdd's if 'or' is present |
|---|---|
| Submitted | 2005-11-06 22:41 UTC |
| From | ken at restivo dot org |
| Assigned | alan_k |
| Status | Closed |
| Package | DB_DataObject |
| PHP Version | 4.3.10 |
| OS | Irrelevant |
| Roadmaps | (Not assigned) |
Comments
[2005-11-06 22:41 UTC] ken at restivo dot org
Description:
------------
If an "or" is present in a whereAdd() condition, it wreaks havoc if the user forgets to enclose it in parens.
i.e. this will not do what the user thinks it'll do:
$obj->whereAdd('foo = "bar"');
$obj->whereAdd('baz = 2 or floop < 2934');
Test script:
---------------
It was an easy fix:
@@ -586,6 +586,9 @@ class DB_DataObject extends DB_DataObjec
return $this->raiseError("WhereAdd: No Valid Arguments", DB_DATAOBJECT_ERROR_INVALIDARGS);
}
$r = $this->_query['condition'];
+ if(stristr($cond, ' or ')){
+ $cond = "({$cond})";
+ }
if ($this->_query['condition']) {
$this->_query['condition'] .= " {$logic} {$cond}";
return $r;
Expected result:
----------------
Which would give the naive user what they'd expect to see:
WHERE foo = "bar" AND (baz = 2 or floop < 2934)'
Actual result:
--------------
Without the patch, whereAdd currently produces:
'WHERE foo = "bar" AND baz = 2 or floop < 2934'
... which (IMHO) certainly violates the principle of least surprise.
This caused some pretty major heartburn in my app, anyway.