From 569348bddaec7144fc1e8b4827b4c982ed070f2f Mon Sep 17 00:00:00 2001 From: zYne Date: Thu, 26 Jul 2007 22:57:36 +0000 Subject: [PATCH] --- lib/Doctrine/Search.php | 2 +- lib/Doctrine/Search/Query.php | 83 ++++++++++++++++++++++++++--------- 2 files changed, 64 insertions(+), 21 deletions(-) diff --git a/lib/Doctrine/Search.php b/lib/Doctrine/Search.php index 3f371b604..01531c4d8 100644 --- a/lib/Doctrine/Search.php +++ b/lib/Doctrine/Search.php @@ -132,7 +132,7 @@ class Doctrine_Search unset($def['sequence']); unset($def['primary']); - $col = strtolower($name . '_' . $column); + $col = strtolower(Doctrine::tableize($name) . '_' . $column); $def['primary'] = true; $fk[$col] = $def; diff --git a/lib/Doctrine/Search/Query.php b/lib/Doctrine/Search/Query.php index 2d8f4849d..cdaa33ab2 100644 --- a/lib/Doctrine/Search/Query.php +++ b/lib/Doctrine/Search/Query.php @@ -37,24 +37,23 @@ class Doctrine_Search_Query */ protected $_query; /** - * @var array $_aliases an array of searchable component aliases + * @var Doctrine_Table $_table the index table */ - protected $_aliases = array(); + protected $_table = array(); + + protected $_sql = ''; /** - * @param Doctrine_Query $query the base query + * @param octrine_Table $_table the index table */ - public function __construct($query) + public function __construct($table) { - if (is_string($query)) { - $this->_query = new Doctrine_Query(); - $this->_query->parseQuery($query); - } elseif ($query instanceof Doctrine_Query) { - $this->_query = $query; - } else { - throw new Doctrine_Exception('Constructor argument should be either Doctrine_Query object or a valid DQL query string'); - } - - $this->_query->getQuery(); + if (is_string($table)) { + $table = Doctrine_Manager::table($table); + } + + $this->_table = $table; + + $this->_query = new Doctrine_Query(); } /** * getQuery @@ -65,13 +64,54 @@ class Doctrine_Search_Query { return $this->_query; } - - public function addAlias($alias) - { - $this->_aliases[] = $alias; - } public function search($text) + { + $text = strtolower(trim($text)); + + $terms = Doctrine_Tokenizer::quoteExplode($text); + + $foreignId = current(array_diff($this->_table->getColumnNames(), array('keyword', 'field', 'position'))); + + $numTerms = count($terms); + switch ($numTerms) { + case 0: + return false; + break; + case 1: + // only one term found, use fast + $select = 'SELECT COUNT(keyword) AS relevance, ' . $foreignId; + $from = 'FROM ' . $this->_table->getTableName(); + + if (strpos($terms[0], "'") === false) { + $where = 'WHERE keyword = ?'; + + $params = array($terms[0]); + } else { + $terms[0] = trim($terms[0], "' "); + + $where = 'WHERE keyword = ?'; + $terms = Doctrine_Tokenizer::quoteExplode($terms[0]); + $params = $terms; + foreach ($terms as $k => $term) { + if ($k === 0) { + continue; + } + $where .= ' AND (position + ' . $k . ') = (SELECT position FROM ' . $this->_table->getTableName() . ' WHERE keyword = ?)'; + } + } + + $groupby = 'GROUP BY ' . $foreignId; + $orderby = 'ORDER BY relevance'; + + break; + default: + + } + + $this->_sql = $select . ' ' . $from . ' ' . $where . ' ' . $groupby . ' ' . $orderby; + } + public function search2($text) { $text = strtolower($text); @@ -107,7 +147,10 @@ class Doctrine_Search_Query $this->_query->addWhere(implode(' OR ', $condition), $terms); } } - + public function getSql() + { + return $this->_sql; + } public function execute() { $resultSet = $this->_query->execute();