Beispiel #1
0
 public function search($query, $fields = array())
 {
     if (!count($fields)) {
         $searchable_fields = array();
         foreach (FW4_Structure::get_searchable_fields() as $field) {
             if ($field->getName() == 'bool') {
                 continue;
             }
             $fields[strval($field['searchable'])] = strval($field['searchable']);
         }
     }
     if (!count($this->order_by)) {
         $this->order_by[] = 'MATCH(`' . implode('`,`', $fields) . '`) AGAINST ("' . $this->escape($query) . '") DESC';
     }
     $this->where[] = '_language = "' . language() . '"';
     $this->where[] = 'MATCH(`' . implode('`,`', $fields) . '`) AGAINST ("' . $this->escape($query) . '") > 0';
     $sql = 'SELECT *, MATCH(`' . implode('`,`', $fields) . '`) AGAINST ("' . $this->escape($query) . '") as score FROM `_search_index` WHERE ' . implode(' AND ', $this->where);
     if (count($this->group_by)) {
         $sql .= ' GROUP BY ' . implode(',', $this->group_by);
     }
     if (count($this->order_by)) {
         $sql .= ' ORDER BY ' . implode(',', $this->order_by);
     }
     if ($this->limit) {
         $sql .= ' LIMIT ' . intval($this->limit * ($this->page - 1)) . ',' . $this->limit;
     }
     $result = new Resultset($this->db->query($sql, PDO::FETCH_CLASS, 'Model', array('translate' => array())), '_search_index', '_search_index');
     $result->_children($this->children);
     $result->_relations($this->relations);
     $result->_fetchable_fields($this->fetchable_fields);
     return $result;
 }