我正在寻找一种在 SugarCRM 中搜索帐户时进行通配符搜索的方法,但我无法让查询正常工作。
这是 build_generic_where_clause() 函数:
function build_generic_where_clause ($the_query_string) {
$where_clauses = Array();
$the_query_string = $this->db->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;
}
我只更改array_push($where_clauses, "accounts.name like '%$the_query_string%'");
为在 the_query_string 的任一侧包含百分号。
这是 view.list.php 中的 processSearchForm():
function processSearchForm(){
if(isset($_REQUEST['query']))
{
// we have a query
if(!empty($_SERVER['HTTP_REFERER']) && preg_match('/action=EditView/', $_SERVER['HTTP_REFERER'])) { // from EditView cancel
$this->searchForm->populateFromArray($this->storeQuery->query);
}
else {
$this->searchForm->populateFromRequest();
}
$where_clauses = $this->searchForm->generateSearchWhere(true, $this->seed->module_dir);
if (count($where_clauses) > 0 )$this->where = '('. implode(' ) AND ( ', $where_clauses) . ')';
$GLOBALS['log']->info("List View Where Clause: $this->where");
$log = fopen('1.txt', "a");
fwrite($log, $this->where . "\n");
}
if($this->use_old_search){
switch($view) {
case 'basic_search':
$this->searchForm->setup();
$this->searchForm->displayBasic($this->headers);
break;
case 'advanced_search':
$this->searchForm->setup();
$this->searchForm->displayAdvanced($this->headers);
break;
case 'saved_views':
echo $this->searchForm->displaySavedViews($this->listViewDefs, $this->lv, $this->headers);
break;
}
}else{
echo $this->searchForm->display($this->headers);
}
}
请注意,我只添加了日志文件写入来捕获 $this->where。如果我使用搜索框查找诸如“Newbold”和“New York Design”之类的帐户,我只会得到“Newbold”,并且我的日志文件显示为(accounts.name like 'new%')
. 因此,第一个百分号以某种方式被删除,我相信 processSearchForm() 某处。很难确定是这种情况还是罪魁祸首是否在其他地方。我发现这段代码有点复杂而且到处都是,但这是我唯一需要做的定制。任何帮助将不胜感激。