Entity/DocumentRepository.php
changeset 42 0e57c730bb18
parent 37 9ba15af20acc
child 57 186c4121c7b3
equal deleted inserted replaced
39:b403086580f7 42:0e57c730bb18
     1 <?php
     1 <?php
     2 
     2 
     3 namespace IRI\Bundle\WikiTagBundle\Entity;
     3 namespace IRI\Bundle\WikiTagBundle\Entity;
       
     4 
       
     5 use IRI\Bundle\WikiTagBundle\Model\DocumentInterface;
     4 
     6 
     5 use Doctrine\ORM\EntityRepository;
     7 use Doctrine\ORM\EntityRepository;
     6 use IRI\Bundle\WikiTagBundle\Entity\Document;
     8 use IRI\Bundle\WikiTagBundle\Entity\Document;
     7 use Doctrine\ORM\Query\ResultSetMapping;
     9 use Doctrine\ORM\Query\ResultSetMapping;
     8 use \ReflectionClass;
    10 use \ReflectionClass;
     9 use Doctrine\ORM\AbstractQuery;
    11 use Doctrine\ORM\AbstractQuery;
    10 use Doctrine\ORM\Mapping\ClassMetadataInfo;
    12 use Doctrine\ORM\Mapping\ClassMetadataInfo;
       
    13 use IRI\Bundle\WikiTagBundle\Model\ModelException;
    11 
    14 
    12 /**
    15 /**
    13  * DocumentRepository
    16  * DocumentRepository
    14  *
    17  *
    15  * This class was generated by the Doctrine ORM. Add your own custom
    18  * This class was generated by the Doctrine ORM. Add your own custom
    16  * repository methods below.
    19  * repository methods below.
    17  */
    20  */
    18 class DocumentRepository extends EntityRepository
    21 class DocumentRepository extends EntityRepository
    19 {
    22 {
    20     /**
    23     /**
    21      *
    24      * The cache for the host document class
    22      * TODO : Enter description here ...
       
    23      * @var ReflectionClass
    25      * @var ReflectionClass
    24      */
    26      */
    25     private $reflection_class;
    27     private $reflection_class;
       
    28     
       
    29     /**
       
    30      * The cache for the wikitag document class
       
    31      * @var ReflectionClass
       
    32      */
    26     private $reflection_doc_class;
    33     private $reflection_doc_class;
    27     private $set_methods = array();
    34     private $set_methods = array();
    28     private $get_methods = array();
    35     private $get_methods = array();
    29     
    36 
    30     function findOneByExternalId($external_id)
    37     /**
       
    38      * Find one wikitagRepository by its externalId i.e. the host document id.
       
    39      * @param $external_id
       
    40      */
       
    41     public function findOneByExternalId($external_id)
    31     {
    42     {
    32         return $this->findOneBy(array("externalId" => $external_id));
    43         return $this->findOneBy(array("externalId" => $external_id));
    33     }
    44     }
       
    45     
    34     
    46     
    35     private function reflectionSetField($object, $method_name, $value)
    47     private function reflectionSetField($object, $method_name, $value)
    36     {
    48     {
    37         if(isset($this->set_methods[$method_name]))
    49         if(isset($this->set_methods[$method_name]))
    38         {
    50         {
   124                 {
   136                 {
   125                     return $get_object->getValue($document);
   137                     return $get_object->getValue($document);
   126                 }
   138                 }
   127                 else
   139                 else
   128                 {
   140                 {
   129                     //TODO : custom exception
   141                     throw new ModelException("Bad reflection object type");
   130                     throw new \Exception("Bad reflection object type");
   142                 }
   131                 }
   143             }
   132             }
   144         }
   133         }
   145         
   134         
   146         throw new ModelException("Unknown accessor $accessor");
   135         //TODO: replace by custom exception
       
   136         throw new \Exception("Unknown accessor $accessor");
       
   137     }
   147     }
   138     
   148     
   139     
   149     
   140     private function getColumnName($field_name)
   150     private function getColumnName($field_name)
   141     {
   151     {
   159         }
   169         }
   160         
   170         
   161         return $res;
   171         return $res;
   162     }
   172     }
   163     
   173     
       
   174     /**
       
   175      * Write a wikitag document given the host document and the field list.
       
   176      * @param $document The source document
       
   177      * @param $document_id_column the name of the source document id column
       
   178      * @param $fields The list of field definition. This is an associative array [<field name>=><field definition>].
       
   179      *     See the @IRI\Bundle\WikiTagBundle\DependencyInjection\Configuration documentation
       
   180      */
   164     public function writeDocument($document,  $document_id_column, $fields)
   181     public function writeDocument($document,  $document_id_column, $fields)
   165     {
   182     {
   166         // get document from id
   183         // get document from id
   167          
   184          
   168         $docid = $this->reflectionGetField($document, $document_id_column);
   185         $docid = $this->reflectionGetField($document, $document_id_column);
   200         $this->getEntityManager()->flush();
   217         $this->getEntityManager()->flush();
   201         return $baseDocument;
   218         return $baseDocument;
   202     
   219     
   203     }
   220     }
   204     
   221     
   205     function removeDocument($document, $document_id_column)
   222     /**
       
   223      * Remove a Wikitag doument given the host docuument.
       
   224      * @param $document The host document
       
   225      * @param string $document_id_column The host document id column name
       
   226      */
       
   227     public function removeDocument($document, $document_id_column)
   206     {
   228     {
   207         $docid = $this->reflectionGetField($document, $document_id_column);
   229         $docid = $this->reflectionGetField($document, $document_id_column);
   208         $baseDocument = $this->findOneByExternalId($docid);
   230         $baseDocument = $this->findOneByExternalId($docid);
   209         if(!is_null($baseDocument))
   231         if(!is_null($baseDocument))
   210         {
   232         {
   211             $this->getEntityManager()->remove($baseDocument);
   233             $this->getEntityManager()->remove($baseDocument);
   212         }
   234         }
   213     }
   235     }
   214     
   236     
   215     function getTagsStr($document)
   237     
       
   238     /**
       
   239      * return the list of a wikitag documents the tags label.
       
   240      * @param DocumentInterface $document the wikitag document
       
   241      * @return array
       
   242      */
       
   243     public function getTagsStr($document)
   216     {
   244     {
   217         $em = $this->getEntityManager();
   245         $em = $this->getEntityManager();
   218         $query = $em->createQuery("SELECT t.label FROM WikiTagBundle:DocumentTag dt JOIN dt.tag t WHERE dt.document = :docid");
   246         $query = $em->createQuery("SELECT t.label FROM WikiTagBundle:DocumentTag dt JOIN dt.tag t WHERE dt.document = :docid");
   219         $query = $query->setParameter("docid", $document);
   247         $query = $query->setParameter("docid", $document);
   220         $result = $query->getScalarResult();
   248         $result = $query->getScalarResult();
   223             $tagstr[] = $res['label'];
   251             $tagstr[] = $res['label'];
   224         }
   252         }
   225         return $tagstr;
   253         return $tagstr;
   226     }
   254     }
   227     
   255     
   228     function updateTagsStr($document)
   256     /**
       
   257      * Update a wikitag document tags string.
       
   258      * @param DocumentInterface $document the wikitag document
       
   259      */
       
   260     function updateTagsStr(DocumentInterface $document)
   229     {
   261     {
   230         
   262         
   231         $tagstr = $this->getTagsStr($document);
   263         $tagstr = $this->getTagsStr($document);
   232         
   264         
   233         $document->setTagsStr(implode(",",$tagstr));
   265         $document->setTagsStr(implode(",",$tagstr));
   234         $this->getEntityManager()->persist($document);
   266         $this->getEntityManager()->persist($document);
   235     }
   267     }
   236     
   268     
   237 
   269 
   238     /**
   270     /**
       
   271      * Search wikitag documents using the index.
   239      *
   272      *
   240      * Enter description here ...
       
   241      * @param array $values : key: the fields to search into, value : array('value'=>value, 'weight'=>weight)
   273      * @param array $values : key: the fields to search into, value : array('value'=>value, 'weight'=>weight)
   242      * @param array $conditions : array : key : field name, value : simple value (operator is "=") or array(valuea, value2,...) (operatr is IN) or array("operator"=>"", "value"=>value)
   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)
   243      * @return Ambigous <multitype:, \Doctrine\ORM\mixed, \Doctrine\DBAL\Driver\Statement, \Doctrine\ORM\Internal\Hydration\mixed>
   275      * @return array [["id" => <the wikitag document id>, "externalId" => <the host document ids>, "score" => <the score for this document>]]
   244      */
   276      */
   245     function search(array $values, array $conditions=NULL)
   277     function search(array $values, array $conditions=NULL)
   246     {
   278     {
   247         $em = $this->getEntityManager();
   279         $em = $this->getEntityManager();
   248         
   280         
   330         $query->setParameters($parameters);
   362         $query->setParameters($parameters);
   331         
   363         
   332         $res = $query->getResult();
   364         $res = $query->getResult();
   333         
   365         
   334         return $res;
   366         return $res;
   335         
       
   336         
       
   337     }
   367     }
   338         
   368         
   339 }
   369 }