Prestashop的ajaxProcessSearchCustomers改进

时间:2018-07-05 07:50:47

标签: php mysql prestashop explode

我是一个修补匠,不是编码专家,但是我管理得很好。 我刚刚将一个庞大的(10000)客户数据库转移到Prestashop实例。它工作得很好,但就我而言,客户搜索功能就很糟糕。我有很多客户,他们有多个姓氏,例如“ van de vel”,“ van san”,“ van den broeck”。搜索功能php_explodes在“”上,因此我得到了数百个结果,并且大多数时候不是正确的结果。

任何人都可以朝着正确的方向引导我,以便我可以使用“ Kristof Van den Broeck”之类的全名进行此功能搜索。

这是功能:

public function ajaxProcessSearchCustomers()
{
    $searches = explode(' ', Tools::getValue('customer_search'));
    $customers = [];
    $searches = array_unique($searches);
    foreach ($searches as $search) {
        if (!empty($search) && $results = Customer::searchByName($search, 50)) {
            foreach ($results as $result) {
                if ($result['active']) {
                    $customers[$result['id_customer']] = $result;
                }
            }
        }
    }

    if (count($customers)) {
        $toReturn = [
            'customers' => $customers,
            'found'     => true,
        ];
    } else {
        $toReturn = ['found' => false];
    }

    $this->content = json_encode($toReturn);
}

这是数据库查询:

public static function searchByName($query, $limit = null)
{
    $sqlBase = 'SELECT * FROM `'._DB_PREFIX_.'customer`';
    $sql = '('.$sqlBase.' WHERE `email` LIKE \'%'.pSQL($query).'%\' '.Shop::addSqlRestriction(Shop::SHARE_CUSTOMER).')';
    $sql .= ' UNION ('.$sqlBase.' WHERE `id_customer` = '.(int) $query.' '.Shop::addSqlRestriction(Shop::SHARE_CUSTOMER).')';
    $sql .= ' UNION ('.$sqlBase.' WHERE `lastname` LIKE \'%'.pSQL($query).'%\' '.Shop::addSqlRestriction(Shop::SHARE_CUSTOMER).')';
    $sql .= ' UNION ('.$sqlBase.' WHERE `firstname` LIKE \'%'.pSQL($query).'%\' '.Shop::addSqlRestriction(Shop::SHARE_CUSTOMER).')';
    $sql .= ' UNION ('.$sqlBase.' WHERE `company` LIKE \'%'.pSQL($query).'%\' '.Shop::addSqlRestriction(Shop::SHARE_CUSTOMER).')';

    if ($limit) {
        $sql .= ' LIMIT 0, '.(int) $limit;
    }

    return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
}

实际上,我对于如何解决这个问题一无所知,因此任何输入都会受到赞赏!

0 个答案:

没有答案