make sure that we use external ids in the controller
authorymh <ymh.work@gmail.com>
Thu, 17 Nov 2011 16:48:19 +0100
changeset 37 9ba15af20acc
parent 36 540607cf3447
child 38 e48c2e503945
make sure that we use external ids in the controller
Controller/WikiTagController.php
Entity/DocumentRepository.php
--- a/Controller/WikiTagController.php	Thu Nov 17 12:07:53 2011 +0100
+++ b/Controller/WikiTagController.php	Thu Nov 17 16:48:19 2011 +0100
@@ -147,7 +147,9 @@
         $id_tag = $this->getRequest()->request->get('tag_id');
         // We get the DocumentTag meant to be deleted, and remove it.
         $em = $this->getDoctrine()->getEntityManager();
-        $dt = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findOneBy(array('tag' => $id_tag, 'document' => $id_doc));
+        //TODO: use external id
+        
+        $dt = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findOneByDocumentExternalId($id_doc, array('tag' => $id_tag));
         $em->remove($dt);
         $em->flush();
 
@@ -167,16 +169,16 @@
         if($tag_label!=$moved_tag->getLabel()){
             // We get the DocumentTags
             $em = $this->getDoctrine()->getEntityManager();
-            $tags = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findBy(array('document' => $id_doc));
-            $nb_tags = count($tags);
+            
+            $tags = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findByDocumentExternalId($id_doc);
             $found = false;
-            $i = 0;
-            while($i<$nb_tags && $found==false){
-                $dt = $tags[$i];
-                if($dt->getTag()->getLabel()==$tag_label){
+            foreach ($tags as $dt)
+            {
+                if($dt->getTag()->getLabel()===$tag_label)
+                {
                     $found = true;
+                    break;
                 }
-                $i++;
             }
             // If the label was found, we sent a bad request
             if($found==true){
@@ -189,11 +191,12 @@
             $created = $ar[2];
             
             // We get the DocumentTag and change its tag
-            $dt = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findOneBy(array('document' => $id_doc, 'tag' => $id_moved_tag));
+            
+            $dt = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findOneByDocumentExternalId($id_doc, array('tag' => $id_moved_tag));
             $dt->setTag($tag);
             $dt->setWikipediaRevisionId($revision_id);
             
-            $score_res = $this->container->get('wiki_tag.search')->search($tag_label, array("id"=>$id_doc));
+            $score_res = $this->container->get('wiki_tag.search')->search($tag_label, array("externalId"=>$id_doc));
             
             if(count($score_res)>0)
             {
@@ -225,6 +228,7 @@
     {
         $id_doc = $this->getRequest()->request->get('wikitag_document_id');
         $res = $this->getDoctrine()->getRepository('WikiTagBundle:Document');
+        
         $doc = $res->findOneByExternalId($id_doc);
         $doc->setManualOrder(false);
         $this->getDoctrine()->getEntityManager()->persist($doc);
@@ -285,7 +289,6 @@
             $new_DT->setOriginalOrder($new_order);
             $new_DT->setTagOrder($new_order);
             $new_DT->setWikipediaRevisionId($revision_id);
-            //TODO ; calculate score
             $em->persist($new_DT);
             $em->flush();
         }
@@ -328,8 +331,7 @@
         
         if($id_doc && $id_doc!=""){
             // We associate the unsemantized tag to the DocumentTag and save datas
-            // TODO: do the request on external id of document
-            $dt = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findOneBy(array('document' => $id_doc, 'tag' => $id_tag));
+            $dt = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findOneByDocumentExternalId($id_doc, array('tag' => $id_tag));
             $dt->setTag($un_tag);
             $em->flush();
             return $this->renderDocTags($id_doc);
--- a/Entity/DocumentRepository.php	Thu Nov 17 12:07:53 2011 +0100
+++ b/Entity/DocumentRepository.php	Thu Nov 17 16:48:19 2011 +0100
@@ -7,6 +7,7 @@
 use Doctrine\ORM\Query\ResultSetMapping;
 use \ReflectionClass;
 use Doctrine\ORM\AbstractQuery;
+use Doctrine\ORM\Mapping\ClassMetadataInfo;
 
 /**
  * DocumentRepository
@@ -135,7 +136,32 @@
         throw new \Exception("Unknown accessor $accessor");
     }
     
-    function writeDocument($document,  $document_id_column, $fields)
+    
+    private function getColumnName($field_name)
+    {
+        if(isset($this->getClassMetadata()->columnNames[$field_name]))
+        {
+            return $this->getClassMetadata()->columnNames[$field_name];
+        }
+        
+        $res = $field_name;
+        if(isset($this->getClassMetadata()->associationMappings[$field_name]))
+        {
+            $association_mapping = $this->getClassMetadata()->associationMappings[$field_name];
+            if(
+                isset($association_mapping['type'])
+                && ( $association_mapping['type'] === ClassMetadataInfo::ONE_TO_ONE || $association_mapping['type'] === ClassMetadataInfo::MANY_TO_ONE)
+                && count($association_mapping['joinColumns']) > 0
+            )
+            {
+                $res = $association_mapping['joinColumns'][0]['name'];
+            }
+        }
+        
+        return $res;
+    }
+    
+    public function writeDocument($document,  $document_id_column, $fields)
     {
         // get document from id
          
@@ -231,7 +257,14 @@
         $i = 0;
         foreach ($values as $fielddef) {
             $i++;
-            $columns = $fielddef["columns"];
+            $field_list = explode(",", $fielddef["columns"]);
+            $column_list = array();
+            foreach($field_list as $field_name)
+            {
+                $column_list[] = $this->getColumnName(trim($field_name));
+            }
+            $columns = join(",", $column_list);
+            
             $value = $fielddef["value"];
             $weight = isset($fielddef["weight"])?$fielddef["weight"]:1.0;
             
@@ -251,6 +284,7 @@
             foreach ($conditions as $field => $conddef)
             {
                 $i++;
+                $col = $this->getColumnName($field);
                 if(is_array($conddef) && isset($conddef['operator']))
                 {
                     $operator = $conddef["operator"];
@@ -274,11 +308,11 @@
                         $parameters["cond_val_$i_$j"] = $values[$j];
                         $in_parameters[] = ":cond_val_$i_$j";
                     }
-                    $cond = "($field IN (".implode(",",$in_parameters)."))";
+                    $cond = "($col IN (".implode(",",$in_parameters)."))";
                 }
                 else
                 {
-                    $cond = "($field $operator :cond_val_$i)";
+                    $cond = "($col $operator :cond_val_$i)";
                     $parameters["cond_val_$i"] = $values;
                 }
                 $conditions_array[] = $cond;