APIv4 - Skip empty leaves in WHERE clause
authorColeman Watts <coleman@civicrm.org>
Thu, 11 Jun 2020 00:41:51 +0000 (20:41 -0400)
committerColeman Watts <coleman@civicrm.org>
Thu, 11 Jun 2020 00:41:54 +0000 (20:41 -0400)
Fixes an annoying problem with search builder
where the api gives a sql error while building your where clause.
The solution is to skip empty leaves, treating them as WIP.

Civi/Api4/Query/Api4SelectQuery.php

index 794df9580a1150b5a0b0da76d5182ea157a97175..92dee1d12867fffeb457fcefae25365497b3822e 100644 (file)
@@ -199,7 +199,10 @@ class Api4SelectQuery extends SelectQuery {
    */
   protected function buildWhereClause() {
     foreach ($this->where as $clause) {
-      $this->query->where($this->treeWalkClauses($clause, 'WHERE'));
+      $sql = $this->treeWalkClauses($clause, 'WHERE');
+      if ($sql) {
+        $this->query->where($sql);
+      }
     }
   }
 
@@ -270,6 +273,10 @@ class Api4SelectQuery extends SelectQuery {
    * @uses composeClause() to generate the SQL etc.
    */
   protected function treeWalkClauses($clause, $type) {
+    // Skip empty leaf.
+    if (in_array($clause[0], ['AND', 'OR', 'NOT']) && empty($clause[1])) {
+      return '';
+    }
     switch ($clause[0]) {
       case 'OR':
       case 'AND':