Entity/DocumentTagRepository.php
changeset 67 989d9e117586
parent 65 ba6b8e38d90e
child 74 901463f9b11c
equal deleted inserted replaced
66:868d0fcdbb99 67:989d9e117586
    15  * 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  * repository methods below.
    16  * repository methods below.
    17  */
    17  */
    18 class DocumentTagRepository extends EntityRepository
    18 class DocumentTagRepository extends EntityRepository
    19 {
    19 {
    20     /**
    20    /**
    21     *  Find ordered tags by document id
    21     *  Find ordered tags by document id
    22     */
    22     */
    23     public function findOrderedTagsForDoc($doc_id)
    23     public function findOrderedTagsForDoc($doc_id)
    24     {
    24     {
    25         return $this->getEntityManager()
    25         return $this->getEntityManager()
    26         ->createQuery("SELECT doctag FROM WikiTagBundle:DocumentTag doctag JOIN doctag.document doc WHERE doc.externalId=: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")
    27         ->setParameter("doc_id", strval($doc_id))
    27         ->setParameter("doc_id", strval($doc_id))
    28         ->getResult();
    28         ->getResult();
    29     }
    29     }
    30     
    30     
       
    31     private function findMaxOrderInUow($entityList, $doc_id, $max_order) {
       
    32         foreach($entityList as $entity) {
       
    33             if(is_a($entity, "\IRI\Bundle\WikiTagBundle\Model\DocumentTagInterface") &&
       
    34             !is_null($entity->getDocument()) &&
       
    35             ( $entity->getDocument() === $doc_id || $entity->getDocument()->getId() === $doc_id ) )
       
    36             {
       
    37                 $max_order = max(array($max_order, $entity->getTagOrder()));
       
    38             }
       
    39         }
       
    40         return $max_order;
       
    41     }
       
    42     
       
    43         
    31     /**
    44     /**
    32      *  Gets the max order of all tags for one document
    45      *  Gets the max order of all tags for one document
    33      */
    46      */
    34     public function getMaxOrder($doc_id)
    47     public function getMaxOrder($doc_id)
    35     {
    48     {
    36         return $this->getEntityManager()
    49         $max_order_res = $this->getEntityManager()
    37         ->createQuery("SELECT MAX(doctag.tagOrder) FROM WikiTagBundle:DocumentTag doctag JOIN doctag.document doc WHERE doc.externalId= :doc_id")
    50             ->createQuery("SELECT MAX(doctag.tagOrder) FROM WikiTagBundle:DocumentTag doctag JOIN doctag.document doc WHERE doc.externalId= :doc_id")
    38         ->setParameter("doc_id", strval($doc_id))
    51             ->setParameter("doc_id", strval($doc_id))
    39         ->getResult();
    52             ->getSingleScalarResult();
    40         
    53         
    41         //todo: look in work unit
    54         $max_order = 0;
       
    55         if(!is_null($max_order_res)) {
       
    56             $max_order = intval($max_order_res);
       
    57         }
       
    58         
       
    59         $uow = $this->getEntityManager()->getUnitOfWork();
       
    60         
       
    61         $max_order = $this->findMaxOrderInUow($uow->getScheduledEntityInsertions(), $doc_id, $max_order);
       
    62         $max_order = $this->findMaxOrderInUow($uow->getScheduledEntityUpdates(), $doc_id, $max_order);
       
    63 
       
    64         return $max_order;
    42     }
    65     }
    43     
       
    44     
    66     
    45     /**
    67     /**
    46      *
    68      *
    47      * Enter description here ...
    69      * Enter description here ...
    48      * @param unknown_type $external_id
    70      * @param unknown_type $external_id
    79     {
   101     {
    80         $qb = $this->createQueryBuilderByDocumentExternalId($external_id, $filter_array);
   102         $qb = $this->createQueryBuilderByDocumentExternalId($external_id, $filter_array);
    81         return $qb->getQuery()->getResult();
   103         return $qb->getQuery()->getResult();
    82     }
   104     }
    83     
   105     
    84     /**
   106    	/**
    85      *
   107      *
    86      * Enter description here ...
   108      * Enter description here ...
    87      * @param unknown_type $external_id
   109      * @param unknown_type $external_id
    88      * @param array $filter_array
   110      * @param array $filter_array
    89      */
   111      */