Changeset 4823

Show
Ignore:
Timestamp:
08/27/08 00:37:17 (5 months ago)
Author:
jwage
Message:

Fixing Searchable behavior for 1.0

Location:
branches/1.0
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • branches/1.0/lib/Doctrine/Search.php

    r4814 r4823  
    6868    } 
    6969 
    70  
    71     /** 
    72      * search  
    73      *  
    74      * @param string $query 
    75      * @return Doctrine_Collection The collection of search results 
    76      */ 
    77     public function search($query) 
     70    /** 
     71     * Searchable keyword search 
     72     *  
     73     * @param string $string Keyword string to search for 
     74     * @param Doctrine_Query $query Query object to alter. Adds where condition to limit the results using the search index 
     75     * @return mixed The Doctrine_Collection or array of ids and relevancy 
     76     */ 
     77    public function search($string, $query = null) 
    7878    { 
    7979        $q = new Doctrine_Search_Query($this->_table); 
    80          
    81         $q->query($query); 
    82          
    83         return $this->_options['connection']->fetchAll($q->getSql(), $q->getParams());; 
     80 
     81        if ($query instanceof Doctrine_Query) { 
     82            $q->query($string, false); 
     83 
     84            $newQuery = $query->copy(); 
     85            $query->getSql(); 
     86            $newQuery->addWhere($query->getRootAlias() . '.id IN(' . $q->getSql() . ')', $q->getParams()); 
     87 
     88            return $newQuery; 
     89        } else { 
     90            $q->query($string); 
     91            return $this->_options['connection']->fetchAll($q->getSql(), $q->getParams()); 
     92        } 
    8493    } 
    8594     
  • branches/1.0/lib/Doctrine/Search/Query.php

    r4332 r4823  
    7171 
    7272 
    73     public function query($text) 
     73    public function query($text, $includeRelevance = true) 
    7474    { 
    7575        $text = trim($text); 
     
    7979        $weighted = false; 
    8080        if (strpos($text, '^') === false) { 
    81             $select = 'SELECT COUNT(keyword) AS relevance, ' . $foreignId; 
    82             $from = 'FROM ' . $this->_table->getTableName(); 
    83         } else { 
    84             // organize terms according weights 
    85             $weighted = true; 
    86  
    87             $select = 'SELECT SUM(sub_relevance) AS relevance, ' . $foreignId; 
    88             $from = 'FROM ' ; 
     81            if ($includeRelevance) { 
     82                $select = 'SELECT COUNT(keyword) AS relevance, ' . $foreignId; 
     83            } else { 
     84                $select = 'SELECT ' . $foreignId; 
     85            } 
     86        } else { 
     87            if ($includeRelevance) { 
     88                $select = 'SELECT SUM(sub_relevance) AS relevance, ' . $foreignId; 
     89            } else { 
     90                $select = 'SELECT ' . $foreignId; 
     91            } 
    8992        } 
    9093         
     94        $from = 'FROM ' . $this->_table->getTableName(); 
    9195        $where = 'WHERE '; 
    9296        $where .= $this->parseClause($text); 
    9397 
    9498        $groupby = 'GROUP BY ' . $foreignId; 
    95         $orderby = 'ORDER BY relevance DESC'; 
    96  
    97         $this->_sql = $select . ' ' . $from . ' ' . $where . ' ' . $groupby . ' ' . $orderby; 
     99        if ($includeRelevance) { 
     100            $orderBy = 'ORDER BY relevance DESC'; 
     101        } else { 
     102            $orderBy = null; 
     103        } 
     104        $this->_sql = $select . ' ' . $from . ' ' . $where . ' ' . $groupby; 
     105        if (isset($orderBy) && $orderBy !== null) { 
     106            $this->_sql .= ' ' . $orderBy; 
     107        } 
    98108    } 
    99109 
  • branches/1.0/lib/Doctrine/Table.php

    r4786 r4823  
    22732273        } 
    22742274 
     2275        // Forward the method on to the record instance and see if it has anything or one of its behaviors 
     2276        try { 
     2277            return call_user_func_array(array($this->getRecordInstance(), $method . 'TableProxy'), $arguments); 
     2278        } catch (Exception $e) {} 
     2279 
    22752280        throw new Doctrine_Table_Exception(sprintf('Unknown method %s::%s', get_class($this), $method)); 
    22762281    } 
  • branches/1.0/lib/Doctrine/Template/Searchable.php

    r4439 r4823  
    6868        $this->_plugin->batchUpdateIndex($limit, $offset); 
    6969    } 
     70 
     71    /** 
     72     * Searchable keyword search proxy for Doctrine_Table 
     73     *  
     74     * @param string $string Keyword string to search for 
     75     * @param Doctrine_Query $query Query object to alter. Adds where condition to limit the results using the search index 
     76     * @return mixed The Doctrine_Collection or array of ids and relevancy 
     77     */ 
     78    public function searchTableProxy($string, $query = null) 
     79    { 
     80        return $this->_plugin->search($string, $query); 
     81    } 
    7082} 
  • branches/1.0/tests/SearchTestCase.php

    r3884 r4823  
    6161 
    6262        $e->save(); 
     63    } 
     64 
     65    public function testSearchFromTableObject() 
     66    { 
     67        $results = Doctrine::getTable('SearchTest')->search('orm'); 
     68        $this->assertEqual($results[0]['id'], 1); 
     69        $query = Doctrine_Query::create() 
     70            ->from('SearchTest s'); 
     71        $query = Doctrine::getTable('SearchTest')->search('orm', $query); 
     72        $this->assertEqual($query->getSql(), 'SELECT s.id AS s__id, s.title AS s__title, s.content AS s__content FROM search_test s WHERE s.id IN(SELECT id FROM search_test_index WHERE keyword = ? GROUP BY id)'); 
     73        $results = $query->fetchArray(); 
     74        $this->assertEqual($results[0]['id'], 1); 
    6375    } 
    6476 
     
    173185        $oQuery = new Doctrine_Search_Query("SearchTest"); 
    174186        $oQuery->query("^test"); 
    175         $this->assertEqual($oQuery->getSql(), "SELECT SUM(sub_relevance) AS relevance, id FROM WHERE keyword = ? GROUP BY id ORDER BY relevance DESC"); 
     187        $this->assertEqual($oQuery->getSql(), "SELECT SUM(sub_relevance) AS relevance, id FROM search_test WHERE keyword = ? GROUP BY id ORDER BY relevance DESC"); 
    176188    } 
    177189}