Frontend\Modules\Search\Engine\Model::getStartsWith PHP Method

getStartsWith() public static method

..
public static getStartsWith ( string $term, string $language = '', integer $limit = 10 ) : array
$term string The first letters of the term we're looking for.
$language string The language to search in.
$limit integer Limit result set.
return array
    public static function getStartsWith($term, $language = '', $limit = 10)
    {
        // language given
        if ($language) {
            return (array) FrontendModel::getContainer()->get('database')->getRecords('SELECT s1.term, s1.num_results
                 FROM search_statistics AS s1
                 INNER JOIN
                 (
                     SELECT term, MAX(id) AS id, language
                     FROM search_statistics
                     WHERE term LIKE ? AND num_results IS NOT NULL AND language = ?
                     GROUP BY term
                 ) AS s2 ON s1.term = s2.term AND s1.id = s2.id AND s1.language = s2.language AND s1.num_results > 0
                 ORDER BY s1.num_results ASC
                 LIMIT ?', array((string) $term . '%', $language, $limit));
        } else {
            // no language given
            return (array) FrontendModel::getContainer()->get('database')->getRecords('SELECT s1.term, s1.num_results
                 FROM search_statistics AS s1
                 INNER JOIN
                 (
                     SELECT term, MAX(id) AS id, language
                     FROM search_statistics
                     WHERE term LIKE ? AND num_results IS NOT NULL
                     GROUP BY term
                 ) AS s2 ON s1.term = s2.term AND s1.id = s2.id AND s1.language = s2.language AND s1.num_results > 0
                 ORDER BY s1.num_results ASC
                 LIMIT ?', array((string) $term . '%', $limit));
        }
    }

Usage Example

Beispiel #1
0
 /**
  * Execute the action
  */
 public function execute()
 {
     // call parent, this will probably add some general CSS/JS or other required files
     parent::execute();
     // get parameters
     $charset = $this->getContainer()->getParameter('kernel.charset');
     $searchTerm = \SpoonFilter::getPostValue('term', null, '');
     $term = $charset == 'utf-8' ? \SpoonFilter::htmlspecialchars($searchTerm) : \SpoonFilter::htmlentities($searchTerm);
     $limit = (int) $this->get('fork.settings')->get('Search', 'autocomplete_num_items', 10);
     // validate
     if ($term == '') {
         $this->output(self::BAD_REQUEST, null, 'term-parameter is missing.');
     } else {
         // get matches
         $matches = FrontendSearchModel::getStartsWith($term, FRONTEND_LANGUAGE, $limit);
         // get search url
         $url = FrontendNavigation::getURLForBlock('Search');
         // loop items and set search url
         foreach ($matches as &$match) {
             $match['url'] = $url . '?form=search&q=' . $match['term'];
         }
         // output
         $this->output(self::OK, $matches);
     }
 }