From 9bf247cc3333a691c45423e9a41cacbed809a5c9 Mon Sep 17 00:00:00 2001 From: Stig Lindqvist Date: Fri, 9 Dec 2011 14:09:07 +0100 Subject: [PATCH] API CHANGE Introduced DataQuery::whereAny() and SQLQuery::whereAny() --- model/DataList.php | 2 +- model/DataQuery.php | 17 +++++++++++++++++ model/SQLQuery.php | 11 ++++++++++- tests/model/SQLQueryTest.php | 9 +++++++++ 4 files changed, 37 insertions(+), 2 deletions(-) mode change 100644 => 100755 tests/model/SQLQueryTest.php diff --git a/model/DataList.php b/model/DataList.php index 09715f921..4a0d65250 100644 --- a/model/DataList.php +++ b/model/DataList.php @@ -292,7 +292,7 @@ class DataList extends ViewableData implements SS_List { $SQL_Statements[] = ('"'.$fieldName.'" != \''.Convert::raw2sql($value).'\''); } } - $this->dataQuery->where(implode(' OR ', $SQL_Statements)); + $this->dataQuery->whereAny($SQL_Statements); return $this; } diff --git a/model/DataQuery.php b/model/DataQuery.php index 655fe51db..f89e5a755 100644 --- a/model/DataQuery.php +++ b/model/DataQuery.php @@ -337,6 +337,23 @@ class DataQuery { return $this; } } + + /** + * Set a WHERE with OR + * + * @param array $filter + * @return DataQuery + * @example $dataQuery->whereAny(array("Monkey = 'Chimp'", "Color = 'Brown'")); + */ + function whereAny($filter) { + if($filter) { + $clone = $this; + $clone->query->whereAny($filter); + return $clone; + } else { + return $this; + } + } /** * Set the ORDER BY clause of this query diff --git a/model/SQLQuery.php b/model/SQLQuery.php index 58eed2989..bfd6b7513 100644 --- a/model/SQLQuery.php +++ b/model/SQLQuery.php @@ -405,7 +405,16 @@ class SQLQuery { return $this; } - + + /** + * + */ + function whereAny($filters) { + if(is_string($filters)) $filters = func_get_args(); + $clause = implode(" OR ", $filters); + return $this->where($clause); + } + /** * Use the disjunctive operator 'OR' to join filter expressions in the WHERE clause. */ diff --git a/tests/model/SQLQueryTest.php b/tests/model/SQLQueryTest.php old mode 100644 new mode 100755 index 7923555a2..fa65c4596 --- a/tests/model/SQLQueryTest.php +++ b/tests/model/SQLQueryTest.php @@ -234,6 +234,15 @@ class SQLQueryTest extends SapphireTest { $query->sql() ); } + + + public function testWhereAny() { + $query = new SQLQuery(); + $query->from( 'MyTable' ); + + $query->whereAny(array("Monkey = 'Chimp'", "Color = 'Brown'")); + $this->assertEquals("SELECT * FROM MyTable WHERE (Monkey = 'Chimp' OR Color = 'Brown')",$query->sql()); + } } class SQLQueryTest_DO extends DataObject implements TestOnly {