Entity/DocumentTagRepository.php
changeset 67 989d9e117586
parent 65 ba6b8e38d90e
child 74 901463f9b11c
--- a/Entity/DocumentTagRepository.php	Mon Dec 19 18:15:42 2011 +0100
+++ b/Entity/DocumentTagRepository.php	Mon Dec 26 22:53:50 2011 +0100
@@ -17,7 +17,7 @@
  */
 class DocumentTagRepository extends EntityRepository
 {
-    /**
+   /**
     *  Find ordered tags by document id
     */
     public function findOrderedTagsForDoc($doc_id)
@@ -28,20 +28,42 @@
         ->getResult();
     }
     
+    private function findMaxOrderInUow($entityList, $doc_id, $max_order) {
+        foreach($entityList as $entity) {
+            if(is_a($entity, "\IRI\Bundle\WikiTagBundle\Model\DocumentTagInterface") &&
+            !is_null($entity->getDocument()) &&
+            ( $entity->getDocument() === $doc_id || $entity->getDocument()->getId() === $doc_id ) )
+            {
+                $max_order = max(array($max_order, $entity->getTagOrder()));
+            }
+        }
+        return $max_order;
+    }
+    
+        
     /**
      *  Gets the max order of all tags for one document
      */
     public function getMaxOrder($doc_id)
     {
-        return $this->getEntityManager()
-        ->createQuery("SELECT MAX(doctag.tagOrder) FROM WikiTagBundle:DocumentTag doctag JOIN doctag.document doc WHERE doc.externalId= :doc_id")
-        ->setParameter("doc_id", strval($doc_id))
-        ->getResult();
+        $max_order_res = $this->getEntityManager()
+            ->createQuery("SELECT MAX(doctag.tagOrder) FROM WikiTagBundle:DocumentTag doctag JOIN doctag.document doc WHERE doc.externalId= :doc_id")
+            ->setParameter("doc_id", strval($doc_id))
+            ->getSingleScalarResult();
         
-        //todo: look in work unit
+        $max_order = 0;
+        if(!is_null($max_order_res)) {
+            $max_order = intval($max_order_res);
+        }
+        
+        $uow = $this->getEntityManager()->getUnitOfWork();
+        
+        $max_order = $this->findMaxOrderInUow($uow->getScheduledEntityInsertions(), $doc_id, $max_order);
+        $max_order = $this->findMaxOrderInUow($uow->getScheduledEntityUpdates(), $doc_id, $max_order);
+
+        return $max_order;
     }
     
-    
     /**
      *
      * Enter description here ...
@@ -81,7 +103,7 @@
         return $qb->getQuery()->getResult();
     }
     
-    /**
+   	/**
      *
      * Enter description here ...
      * @param unknown_type $external_id