2

我正在寻找一种在 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() 某处。很难确定是这种情况还是罪魁祸首是否在其他地方。我发现这段代码有点复杂而且到处都是,但这是我唯一需要做的定制。任何帮助将不胜感激。

4

1 回答 1

0

您应该能够在不更改任何代码的情况下执行此操作。当您从帐户列表视图中搜索时,只需将通配符添加到表单中的搜索中。将“%$the_query_string%”放入搜索表单。

如果您想以编程方式使用通配符来搜索记录,您可以执行以下操作。

<?php

$filter = 'ABC%'; // account names that start with 'ABC'
$account = BeanFactory::getBean('Accounts');
$accounts = $account->get_list("", "accounts.name like '".$filter."'");

foreach ($accounts['list'] as $account)
{
    // do something with $account
}

get_list() 将提取您需要的内容。需要注意的一点是,get_list() 只会返回您要返回列表视图的记录数。因此,如果您的列表视图仅显示 20 条记录,则 get_list() 将返回最多 20 条记录。如果要获取所有结果,请使用 get_full_list()。

于 2013-08-24T20:17:53.547 回答