Entity/DocumentRepository.php
changeset 68 e7384fb35f7a
parent 67 989d9e117586
child 74 901463f9b11c
equal deleted inserted replaced
67:989d9e117586 68:e7384fb35f7a
    56             if(is_null($this->reflection_doc_class))
    56             if(is_null($this->reflection_doc_class))
    57             {
    57             {
    58                 $this->reflection_doc_class = new ReflectionClass(get_class($object));
    58                 $this->reflection_doc_class = new ReflectionClass(get_class($object));
    59             }
    59             }
    60             
    60             
    61             $set_method = NULL;
    61             $set_method = null;
    62             if($this->reflection_doc_class->hasMethod($method_name))
    62             if($this->reflection_doc_class->hasMethod($method_name))
    63             {
    63             {
    64                 $set_method = $this->reflection_doc_class->getMethod($method_name);
    64                 $set_method = $this->reflection_doc_class->getMethod($method_name);
    65             }
    65             }
    66             if(!is_null($set_method))
    66             if(!is_null($set_method))
   152         if(isset($this->getClassMetadata()->columnNames[$field_name]))
   152         if(isset($this->getClassMetadata()->columnNames[$field_name]))
   153         {
   153         {
   154             return $this->getClassMetadata()->columnNames[$field_name];
   154             return $this->getClassMetadata()->columnNames[$field_name];
   155         }
   155         }
   156         
   156         
   157         $res = $field_name;
   157         $res = null;
   158         if(isset($this->getClassMetadata()->associationMappings[$field_name]))
   158         if(isset($this->getClassMetadata()->associationMappings[$field_name]))
   159         {
   159         {
   160             $association_mapping = $this->getClassMetadata()->associationMappings[$field_name];
   160             $association_mapping = $this->getClassMetadata()->associationMappings[$field_name];
   161             if(
   161             if(
   162                 isset($association_mapping['type'])
   162                 isset($association_mapping['type'])
   166             {
   166             {
   167                 $res = $association_mapping['joinColumns'][0]['name'];
   167                 $res = $association_mapping['joinColumns'][0]['name'];
   168             }
   168             }
   169         }
   169         }
   170         
   170         
       
   171         if(is_null($res)) {
       
   172             throw new \Exception("WikiTag.DocumentRepository: Unknown field $field_name");
       
   173         }
   171         return $res;
   174         return $res;
   172     }
   175     }
   173     
   176     
   174     /**
   177     /**
   175      * Write a wikitag document given the host document and the field list.
   178      * Write a wikitag document given the host document and the field list.
   269 
   272 
   270     /**
   273     /**
   271      * Search wikitag documents using the index.
   274      * Search wikitag documents using the index.
   272      *
   275      *
   273      * @param array $values : key: the fields to search into, value : array('value'=>value, 'weight'=>weight)
   276      * @param array $values : key: the fields to search into, value : array('value'=>value, 'weight'=>weight)
   274      * @param array $conditions : array : key : field name, value : simple value (operator is "=") or array(valuea, value2,...) (operatr is IN) or array("operator"=>"", "value"=>value)
   277      * @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)
       
   278      * @param array $fields : array : a list of field name to include in the result
   275      * @return array [["id" => <the wikitag document id>, "externalId" => <the host document ids>, "score" => <the score for this document>]]
   279      * @return array [["id" => <the wikitag document id>, "externalId" => <the host document ids>, "score" => <the score for this document>]]
   276      */
   280      */
   277     function search(array $values, array $conditions=NULL)
   281     function search(array $values, array $conditions=null, array $fields=null)
   278     {
   282     {
   279         $em = $this->getEntityManager();
   283         $em = $this->getEntityManager();
   280         
   284         
       
   285         if(is_null($fields)) {
       
   286             $fieldnamelist = array();
       
   287         }
       
   288         else {
       
   289             $fieldnamelist = $fields;
       
   290         }
       
   291 
   281         $rsm = new ResultSetMapping();
   292         $rsm = new ResultSetMapping();
   282         $rsm->addEntityResult("IRI\Bundle\WikiTagBundle\Entity\Document", "d");
   293         $rsm->addEntityResult("IRI\Bundle\WikiTagBundle\Entity\Document", "d");
   283         $rsm->addFieldResult("d", "id", "id");
   294         $rsm->addFieldResult("d", "id", "id");
       
   295         foreach($fieldnamelist as $fieldname) {
       
   296             $rsm->addFieldResult("d", $fieldname, $fieldname);
       
   297         }
   284         $rsm->addScalarResult("score", "score");
   298         $rsm->addScalarResult("score", "score");
   285         $rsm->addMetaResult("d", "external_id", "externalId");
   299         $rsm->addMetaResult("d", "external_id", "external_id");
   286         
   300         
   287         
   301         
   288         $score = array();
   302         $score = array();
   289         $i = 0;
   303         $i = 0;
   290         foreach ($values as $fielddef) {
   304         foreach ($values as $fielddef) {
   313         {
   327         {
   314             $conditions_array = array();
   328             $conditions_array = array();
   315             $i = 0;
   329             $i = 0;
   316             foreach ($conditions as $field => $conddef)
   330             foreach ($conditions as $field => $conddef)
   317             {
   331             {
       
   332                 if(!$field) {
       
   333                     continue;
       
   334                 }
   318                 $i++;
   335                 $i++;
   319                 $col = $this->getColumnName($field);
   336                 $col = $this->getColumnName($field);
   320                 if(is_array($conddef) && isset($conddef['operator']))
   337                 if(is_array($conddef) && isset($conddef['operator']))
   321                 {
   338                 {
   322                     $operator = $conddef["operator"];
   339                     $operator = $conddef["operator"];
   329                 }
   346                 }
   330                 else
   347                 else
   331                 {
   348                 {
   332                     $operator = "=";
   349                     $operator = "=";
   333                     $values = $conddef;
   350                     $values = $conddef;
       
   351                 }
       
   352                 
       
   353                 if(!in_array( strtolower($operator), array("=","!=","<".">","<=".">=","like","ilike"))) {
       
   354                     throw new Exception('DocumentRepository.search : operator must be in "=","!=","<".">","<=".">=","like","ilike"');
   334                 }
   355                 }
   335                    
   356                    
   336                 if($operator === "IN")
   357                 if($operator === "IN")
   337                 {
   358                 {
   338                     $in_parameters = array();
   359                     $in_parameters = array();
   354             {
   375             {
   355                 $conditions_str = " AND ".implode(" AND ", $conditions_array);
   376                 $conditions_str = " AND ".implode(" AND ", $conditions_array);
   356             }
   377             }
   357                 
   378                 
   358         }
   379         }
   359         
   380         $fieldnamequery = "d.id, d.external_id";
   360         $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);
   381         if(count($fieldnamelist) > 0) {
       
   382             $fieldnamequery .= ", d.".join(", d.", $fieldnamelist);
       
   383         }
       
   384         $query = $em->createNativeQuery("SELECT $fieldnamequery, $score_def AS score FROM wikitag_document d WHERE $score_def > 0  $conditions_str ORDER BY score DESC", $rsm);
   361         
   385         
   362         $query->setParameters($parameters);
   386         $query->setParameters($parameters);
   363         
   387         
   364         $res = $query->getResult();
   388         $res = $query->getResult();
   365         
   389