Entity/DocumentRepository.php
changeset 68 e7384fb35f7a
parent 67 989d9e117586
child 74 901463f9b11c
--- a/Entity/DocumentRepository.php	Mon Dec 26 22:53:50 2011 +0100
+++ b/Entity/DocumentRepository.php	Mon Jan 23 00:48:55 2012 +0100
@@ -58,7 +58,7 @@
                 $this->reflection_doc_class = new ReflectionClass(get_class($object));
             }
             
-            $set_method = NULL;
+            $set_method = null;
             if($this->reflection_doc_class->hasMethod($method_name))
             {
                 $set_method = $this->reflection_doc_class->getMethod($method_name);
@@ -154,7 +154,7 @@
             return $this->getClassMetadata()->columnNames[$field_name];
         }
         
-        $res = $field_name;
+        $res = null;
         if(isset($this->getClassMetadata()->associationMappings[$field_name]))
         {
             $association_mapping = $this->getClassMetadata()->associationMappings[$field_name];
@@ -168,6 +168,9 @@
             }
         }
         
+        if(is_null($res)) {
+            throw new \Exception("WikiTag.DocumentRepository: Unknown field $field_name");
+        }
         return $res;
     }
     
@@ -271,18 +274,29 @@
      * Search wikitag documents using the index.
      *
      * @param array $values : key: the fields to search into, value : array('value'=>value, 'weight'=>weight)
-     * @param array $conditions : array : key : field name, value : simple value (operator is "=") or array(valuea, value2,...) (operatr is IN) or array("operator"=>"", "value"=>value)
+     * @param array $conditions : array : key : field name, value : simple value (operator is "=") or array(valuea, value2,...) (operatr is IN) or array("operator"=>"=","!=","<".">","<=".">=","like","ilike","in">, "value"=>value)
+     * @param array $fields : array : a list of field name to include in the result
      * @return array [["id" => <the wikitag document id>, "externalId" => <the host document ids>, "score" => <the score for this document>]]
      */
-    function search(array $values, array $conditions=NULL)
+    function search(array $values, array $conditions=null, array $fields=null)
     {
         $em = $this->getEntityManager();
         
+        if(is_null($fields)) {
+            $fieldnamelist = array();
+        }
+        else {
+            $fieldnamelist = $fields;
+        }
+
         $rsm = new ResultSetMapping();
         $rsm->addEntityResult("IRI\Bundle\WikiTagBundle\Entity\Document", "d");
         $rsm->addFieldResult("d", "id", "id");
+        foreach($fieldnamelist as $fieldname) {
+            $rsm->addFieldResult("d", $fieldname, $fieldname);
+        }
         $rsm->addScalarResult("score", "score");
-        $rsm->addMetaResult("d", "external_id", "externalId");
+        $rsm->addMetaResult("d", "external_id", "external_id");
         
         
         $score = array();
@@ -315,6 +329,9 @@
             $i = 0;
             foreach ($conditions as $field => $conddef)
             {
+                if(!$field) {
+                    continue;
+                }
                 $i++;
                 $col = $this->getColumnName($field);
                 if(is_array($conddef) && isset($conddef['operator']))
@@ -332,6 +349,10 @@
                     $operator = "=";
                     $values = $conddef;
                 }
+                
+                if(!in_array( strtolower($operator), array("=","!=","<".">","<=".">=","like","ilike"))) {
+                    throw new Exception('DocumentRepository.search : operator must be in "=","!=","<".">","<=".">=","like","ilike"');
+                }
                    
                 if($operator === "IN")
                 {
@@ -356,8 +377,11 @@
             }
                 
         }
-        
-        $query = $em->createNativeQuery("SELECT d.id, d.external_id, $score_def AS score FROM wikitag_document d WHERE $score_def > 0  $conditions_str ORDER BY score DESC", $rsm);
+        $fieldnamequery = "d.id, d.external_id";
+        if(count($fieldnamelist) > 0) {
+            $fieldnamequery .= ", d.".join(", d.", $fieldnamelist);
+        }
+        $query = $em->createNativeQuery("SELECT $fieldnamequery, $score_def AS score FROM wikitag_document d WHERE $score_def > 0  $conditions_str ORDER BY score DESC", $rsm);
         
         $query->setParameters($parameters);