Author: jwage
Date: 2008-08-27 00:37:17 +0100 (Wed, 27 Aug 2008)
New Revision: 4823

Modified:
   branches/1.0/lib/Doctrine/Search.php
   branches/1.0/lib/Doctrine/Search/Query.php
   branches/1.0/lib/Doctrine/Table.php
   branches/1.0/lib/Doctrine/Template/Searchable.php
   branches/1.0/tests/SearchTestCase.php
Log:
Fixing Searchable behavior for 1.0


Modified: branches/1.0/lib/Doctrine/Search/Query.php
===================================================================
--- branches/1.0/lib/Doctrine/Search/Query.php  2008-08-26 22:07:24 UTC (rev 
4822)
+++ branches/1.0/lib/Doctrine/Search/Query.php  2008-08-26 23:37:17 UTC (rev 
4823)
@@ -70,7 +70,7 @@
     }
 
 
-    public function query($text)
+    public function query($text, $includeRelevance = true)
     {
         $text = trim($text);
 
@@ -78,23 +78,33 @@
 
         $weighted = false;
         if (strpos($text, '^') === false) {
-            $select = 'SELECT COUNT(keyword) AS relevance, ' . $foreignId;
-            $from = 'FROM ' . $this->_table->getTableName();
+            if ($includeRelevance) {
+                $select = 'SELECT COUNT(keyword) AS relevance, ' . $foreignId;
+            } else {
+                $select = 'SELECT ' . $foreignId;
+            }
         } else {
-            // organize terms according weights
-            $weighted = true;
-
-            $select = 'SELECT SUM(sub_relevance) AS relevance, ' . $foreignId;
-            $from = 'FROM ' ;
+            if ($includeRelevance) {
+                $select = 'SELECT SUM(sub_relevance) AS relevance, ' . 
$foreignId;
+            } else {
+                $select = 'SELECT ' . $foreignId;
+            }
         }
         
+        $from = 'FROM ' . $this->_table->getTableName();
         $where = 'WHERE ';
         $where .= $this->parseClause($text);
 
         $groupby = 'GROUP BY ' . $foreignId;
-        $orderby = 'ORDER BY relevance DESC';
-
-        $this->_sql = $select . ' ' . $from . ' ' . $where . ' ' . $groupby . 
' ' . $orderby;
+        if ($includeRelevance) {
+            $orderBy = 'ORDER BY relevance DESC';
+        } else {
+            $orderBy = null;
+        }
+        $this->_sql = $select . ' ' . $from . ' ' . $where . ' ' . $groupby;
+        if (isset($orderBy) && $orderBy !== null) {
+            $this->_sql .= ' ' . $orderBy;
+        }
     }
 
     public function parseClause($originalClause, $recursive = false)

Modified: branches/1.0/lib/Doctrine/Search.php
===================================================================
--- branches/1.0/lib/Doctrine/Search.php        2008-08-26 22:07:24 UTC (rev 
4822)
+++ branches/1.0/lib/Doctrine/Search.php        2008-08-26 23:37:17 UTC (rev 
4823)
@@ -67,20 +67,29 @@
         }
     }
 
-
     /**
-     * search 
+     * Searchable keyword search
      * 
-     * @param string $query
-     * @return Doctrine_Collection The collection of search results
+     * @param string $string Keyword string to search for
+     * @param Doctrine_Query $query Query object to alter. Adds where 
condition to limit the results using the search index
+     * @return mixed The Doctrine_Collection or array of ids and relevancy
      */
-    public function search($query)
+    public function search($string, $query = null)
     {
         $q = new Doctrine_Search_Query($this->_table);
-        
-        $q->query($query);
-        
-        return $this->_options['connection']->fetchAll($q->getSql(), 
$q->getParams());;
+
+        if ($query instanceof Doctrine_Query) {
+            $q->query($string, false);
+
+            $newQuery = $query->copy();
+            $query->getSql();
+            $newQuery->addWhere($query->getRootAlias() . '.id IN(' . 
$q->getSql() . ')', $q->getParams());
+
+            return $newQuery;
+        } else {
+            $q->query($string);
+            return $this->_options['connection']->fetchAll($q->getSql(), 
$q->getParams());
+        }
     }
     
     /**

Modified: branches/1.0/lib/Doctrine/Table.php
===================================================================
--- branches/1.0/lib/Doctrine/Table.php 2008-08-26 22:07:24 UTC (rev 4822)
+++ branches/1.0/lib/Doctrine/Table.php 2008-08-26 23:37:17 UTC (rev 4823)
@@ -2272,6 +2272,11 @@
             }
         }
 
+        // Forward the method on to the record instance and see if it has 
anything or one of its behaviors
+        try {
+            return call_user_func_array(array($this->getRecordInstance(), 
$method . 'TableProxy'), $arguments);
+        } catch (Exception $e) {}
+
         throw new Doctrine_Table_Exception(sprintf('Unknown method %s::%s', 
get_class($this), $method));
     }
 }

Modified: branches/1.0/lib/Doctrine/Template/Searchable.php
===================================================================
--- branches/1.0/lib/Doctrine/Template/Searchable.php   2008-08-26 22:07:24 UTC 
(rev 4822)
+++ branches/1.0/lib/Doctrine/Template/Searchable.php   2008-08-26 23:37:17 UTC 
(rev 4823)
@@ -67,4 +67,16 @@
     {
         $this->_plugin->batchUpdateIndex($limit, $offset);
     }
+
+    /**
+     * Searchable keyword search proxy for Doctrine_Table
+     * 
+     * @param string $string Keyword string to search for
+     * @param Doctrine_Query $query Query object to alter. Adds where 
condition to limit the results using the search index
+     * @return mixed The Doctrine_Collection or array of ids and relevancy
+     */
+    public function searchTableProxy($string, $query = null)
+    {
+        return $this->_plugin->search($string, $query);
+    }
 }
\ No newline at end of file

Modified: branches/1.0/tests/SearchTestCase.php
===================================================================
--- branches/1.0/tests/SearchTestCase.php       2008-08-26 22:07:24 UTC (rev 
4822)
+++ branches/1.0/tests/SearchTestCase.php       2008-08-26 23:37:17 UTC (rev 
4823)
@@ -62,6 +62,18 @@
         $e->save();
     }
 
+    public function testSearchFromTableObject()
+    {
+        $results = Doctrine::getTable('SearchTest')->search('orm');
+        $this->assertEqual($results[0]['id'], 1);
+        $query = Doctrine_Query::create()
+            ->from('SearchTest s');
+        $query = Doctrine::getTable('SearchTest')->search('orm', $query);
+        $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)');
+        $results = $query->fetchArray();
+        $this->assertEqual($results[0]['id'], 1);
+    }
+
     public function testQuerying()
     {
         $q = new Doctrine_Query();
@@ -172,6 +184,6 @@
     {
         $oQuery = new Doctrine_Search_Query("SearchTest");
         $oQuery->query("^test");
-        $this->assertEqual($oQuery->getSql(), "SELECT SUM(sub_relevance) AS 
relevance, id FROM  WHERE keyword = ? GROUP BY id ORDER BY relevance DESC");
+        $this->assertEqual($oQuery->getSql(), "SELECT SUM(sub_relevance) AS 
relevance, id FROM search_test WHERE keyword = ? GROUP BY id ORDER BY relevance 
DESC");
     }
 }


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"doctrine-svn" group.
 To post to this group, send email to [email protected]
 To unsubscribe from this group, send email to [EMAIL PROTECTED]
 For more options, visit this group at 
http://groups.google.co.uk/group/doctrine-svn?hl=en-GB
-~----------~----~----~----~------~----~------~--~---

Reply via email to