title: Account Search Wildcard - SugarCRM
body: <p>I'm looking for a way to do a wildcard search when searching for accounts in SugarCRM but I'm having trouble getting the queries to work properly. </p>
  
  <p>Here's the build_generic_where_clause() function:</p>
  
  <pre><code>function build_generic_where_clause ($the_query_string) {
      $where_clauses = Array();
      $the_query_string = $this-&gt;db-&gt;quote($the_query_string);
      array_push($where_clauses, "accounts.name like '%$the_query_string%'");
      if (is_numeric($the_query_string)) {
          array_push($where_clauses, "accounts.phone_alternate like '%$the_query_string%'");
          array_push($where_clauses, "accounts.phone_fax like '%$the_query_string%'");
          array_push($where_clauses, "accounts.phone_office like '%$the_query_string%'");
      }
  
      $the_where = "";
      foreach($where_clauses as $clause)
      {
          if(!empty($the_where)) $the_where .= " or ";
          $the_where .= $clause;
      }
  
  
      $log = fopen('1.txt', "a");
      fwrite($log, $the_where . "\n");
  
      return $the_where;
  }
  </code></pre>
  
  <p>I only changed <code>array_push($where_clauses, "accounts.name like '%$the_query_string%'");</code> to include the percentage signs on either side of the_query_string. </p>
  
  <p>Here's processSearchForm() from view.list.php:</p>
  
  <pre><code> function processSearchForm(){
  
  
          if(isset($_REQUEST['query']))
          {
              // we have a query
              if(!empty($_SERVER['HTTP_REFERER']) &amp;&amp; preg_match('/action=EditView/', $_SERVER['HTTP_REFERER'])) { // from EditView cancel
                  $this-&gt;searchForm-&gt;populateFromArray($this-&gt;storeQuery-&gt;query);
              }
              else {
                  $this-&gt;searchForm-&gt;populateFromRequest();
              }
  
              $where_clauses = $this-&gt;searchForm-&gt;generateSearchWhere(true, $this-&gt;seed-&gt;module_dir);
  
              if (count($where_clauses) &gt; 0 )$this-&gt;where = '('. implode(' ) AND ( ', $where_clauses) . ')';
              $GLOBALS['log']-&gt;info("List View Where Clause: $this-&gt;where");
  
              $log = fopen('1.txt', "a");
              fwrite($log, $this-&gt;where . "\n");
  
          }
          if($this-&gt;use_old_search){
              switch($view) {
                  case 'basic_search':
                      $this-&gt;searchForm-&gt;setup();
                      $this-&gt;searchForm-&gt;displayBasic($this-&gt;headers);
                      break;
                   case 'advanced_search':
                      $this-&gt;searchForm-&gt;setup();
                      $this-&gt;searchForm-&gt;displayAdvanced($this-&gt;headers);
                      break;
                   case 'saved_views':
                      echo $this-&gt;searchForm-&gt;displaySavedViews($this-&gt;listViewDefs, $this-&gt;lv, $this-&gt;headers);
                     break;
              }
          }else{
              echo $this-&gt;searchForm-&gt;display($this-&gt;headers);
          }
      }
  </code></pre>
  
  <p>Note that I only added the log file write to catch the $this->where. If I use the searchbox to find an account such as "Newbold" as well as "New York Design", I only get "Newbold" as a result and my log file reads <code>(accounts.name like 'new%')</code>. So the first percentage sign is being removed somehow or another, I believe in the processSearchForm() somewhere. It's tough to figure out if that's the case or if the culprit lies elsewhere. I find this code to be a bit convoluted and all over the place, but this is the only customization I need done. Any help would be immensely appreciated. </p>