Entity/DocumentTagRepository.php
changeset 5 45378793512a
parent 2 13f43f53d0ba
child 8 7d2fb5d7c9ff
equal deleted inserted replaced
4:e63ac93fdbde 5:45378793512a
     1 <?php
     1 <?php
     2 
     2 
     3 namespace IRI\Bundle\WikiTagBundle\Entity;
     3 namespace IRI\Bundle\WikiTagBundle\Entity;
     4 
     4 
       
     5 use Doctrine\ORM\Query;
       
     6 
       
     7 use Doctrine\ORM\QueryBuilder;
       
     8 
     5 use Doctrine\ORM\EntityRepository;
     9 use Doctrine\ORM\EntityRepository;
       
    10 use Doctrine\ORM\NoResultException;
     6 
    11 
     7 /**
    12 /**
     8  * DocumentTagRepository
    13  * DocumentTagRepository
     9  *
    14  *
    10  * This class was generated by the Doctrine ORM. Add your own custom
    15  * This class was generated by the Doctrine ORM. Add your own custom
    16     *  Find ordered tags by document id
    21     *  Find ordered tags by document id
    17     */
    22     */
    18     public function findOrderedTagsForDoc($doc_id)
    23     public function findOrderedTagsForDoc($doc_id)
    19     {
    24     {
    20         return $this->getEntityManager()
    25         return $this->getEntityManager()
    21         ->createQuery("SELECT doctag FROM WikiTagBundle:DocumentTag doctag WHERE doctag.document=:doc_id ORDER BY doctag.tagOrder ASC")
    26         ->createQuery("SELECT doctag FROM WikiTagBundle:DocumentTag doctag JOIN doctag.document doc WHERE doc.externalId=:doc_id ORDER BY doctag.tagOrder ASC")
    22         ->setParameter("doc_id", $doc_id)
    27         ->setParameter("doc_id", strval($doc_id))
    23         ->getResult();
    28         ->getResult();
    24     }
    29     }
    25     
    30     
    26     /**
    31     /**
    27      *  Gets the max order of all tags for one document
    32      *  Gets the max order of all tags for one document
    28      */
    33      */
    29     public function getMaxOrder($doc_id)
    34     public function getMaxOrder($doc_id)
    30     {
    35     {
    31         return $this->getEntityManager()
    36         return $this->getEntityManager()
    32         ->createQuery("SELECT MAX(doctag.tagOrder) FROM WikiTagBundle:DocumentTag doctag  WHERE doctag.document= :doc_id")
    37         ->createQuery("SELECT MAX(doctag.tagOrder) FROM WikiTagBundle:DocumentTag doctag JOIN doctag.document doc WHERE doc.externalId= :doc_id")
    33         ->setParameter("doc_id", $doc_id)
    38         ->setParameter("doc_id", strval($doc_id))
    34         ->getResult();
    39         ->getResult();
    35     }
    40     }
    36     
    41     
       
    42     
       
    43     /**
       
    44      *
       
    45      * Enter description here ...
       
    46      * @param unknown_type $external_id
       
    47      * @param array $filter_array
       
    48      * @return QueryBuilder
       
    49      */
       
    50     private function createQueryBuilderByDocumentExternalId($external_id, array $filter_array=null) {
       
    51         $qb = $this->createQueryBuilder("dt")
       
    52         ->join('dt.document', 'd')
       
    53         ->where('d.externalId = :external_id');
       
    54 
       
    55         $params = array("external_id"=>strval($external_id));
       
    56         
       
    57         if(!is_null($filter_array)) {
       
    58             foreach ($filter_array as $key => $value) {
       
    59                 $qb = $qb->where("dt.$key = :p_$key");
       
    60                 $params["p_$key"] = $value;
       
    61             }
       
    62         }
       
    63         
       
    64         $qb = $qb->setParameters($params);
       
    65         
       
    66         return $qb;
       
    67     }
       
    68     
       
    69     public function findByDocumentExternalId($external_id, array $filter_array=null)
       
    70     {
       
    71         $qb = $this->createQueryBuilderByDocumentExternalId($external_id, $filter_array);
       
    72         return $qb->getQuery()->getResult();
       
    73     }
       
    74     
       
    75     public function findOneByDocumentExternalId($external_id, array $filter_array=null) {
       
    76         
       
    77         $qb = $this->createQueryBuilderByDocumentExternalId($external_id, $filter_array)->setMaxResults(1);
       
    78         
       
    79         try {
       
    80             return $qb->getQuery()->getSingleResult();
       
    81         } catch (NoResultException $e) {
       
    82             return null;
       
    83         }
       
    84     }
       
    85     
    37 }
    86 }