帐户搜索通配符 - SugarCRM

时间:2013-04-23 21:47:10

标签: php mysql sugarcrm

我正在寻找一种在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);
        }
    }

请注意,我只添加了日志文件write来捕获$ this->其中。如果我使用搜索框查找“Newbold”以及“纽约设计”等帐户,我只会得到“Newbold”,而我的日志文件会显示(accounts.name like 'new%')。所以第一个百分号是以某种方式被删除的,我相信在某处的processSearchForm()。很难弄清楚是否是这种情况,或者罪魁祸首在于其他地方。我觉得这个代码有点复杂,而且到处都是,但这是我需要做的唯一定制。任何帮助都会非常感激。

1 个答案:

答案 0 :(得分: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()。

相关问题