Update ORM configuration to allow JOIN between Tag and DocumentTag. TagList template has now the number of documents by tag.
authorcavaliet
Wed, 26 Oct 2011 15:38:05 +0200
changeset 14 673b2766024e
parent 13 c288952a089f
child 15 ab71cf8bff55
Update ORM configuration to allow JOIN between Tag and DocumentTag. TagList template has now the number of documents by tag.
Controller/WikiTagController.php
Model/BaseDocumentInterface.php
Model/Document.php
Model/DocumentInterface.php
Model/Tag.php
Model/TagInterface.php
Resources/config/doctrine/Document.orm.yml
Resources/config/doctrine/DocumentTag.orm.yml
Resources/config/doctrine/Tag.orm.yml
Resources/views/WikiTag/TagListTable.html.twig
--- a/Controller/WikiTagController.php	Tue Oct 25 12:20:47 2011 +0200
+++ b/Controller/WikiTagController.php	Wed Oct 26 15:38:05 2011 +0200
@@ -473,7 +473,8 @@
      */
     public function renderAllTags($num_page=NULL, $nb_by_page=NULL, $sort=NULL, $searched=NULL)
     {
-        // We get the needed datas in an array($tags, $num_page, $nb_by_page, $searched, $sort, $reverse_sort, $pagerfanta);
+        
+        //We get the needed datas in an array($tags, $num_page, $nb_by_page, $searched, $sort, $reverse_sort, $pagerfanta);
         $ar = $this->getAllTags($num_page, $nb_by_page, $sort, $searched);
         $tags = $ar[0];
         $num_page = $ar[1];
@@ -484,6 +485,8 @@
         
         return $this->render('WikiTagBundle:WikiTag:TagListTable.html.twig', 
             array('tags' => $tags, 'searched' => $searched, 'nb_by_page' => $nb_by_page, 'sort' => $sort, 'num_page' => $num_page, 'reverse_sort' => $reverse_sort));
+        
+        return $this->getAllTags();
     }
 
 
@@ -508,12 +511,17 @@
         
         // We build the query.
         $qb = $this->getDoctrine()->getEntityManager()->createQueryBuilder();
-        $qb->select('t')->from('WikiTagBundle:Tag','t');
+        $qb->select('t', 'COUNT( dt.id ) AS nb_docs');
+        $qb->from('WikiTagBundle:Tag','t');
+        $qb->leftJoin('t.documents', 'dt', 'WITH', 't = dt.tag');
+        $qb->addGroupBy('t.id');
+        
         // We add the search string if necessary ('* bugs)
         if($searched!="" && $searched!="'*"){
             // We replace "*" by "%".
             $qb->where($qb->expr()->orx($qb->expr()->like('t.normalizedLabel', "'".str_replace("*", "%", $searched)."'")));
         }
+        
         // We add the sorting criteria
         if($sort==NULL){
             $sort = "popd"; // sort by descendent popularity by default.
@@ -549,12 +557,10 @@
         $adapter = new DoctrineORMAdapter($qb);
         $pagerfanta = new Pagerfanta($adapter);
         $pagerfanta->setMaxPerPage($nb_by_page); // 10 by default
-        //$last_page = $pagerfanta->getNbPages();
         $pagerfanta->setCurrentPage($num_page); // 1 by default
         $nb_total = $pagerfanta->getNbResults();
         $tags = $pagerfanta->getCurrentPageResults();
         $pagerfanta->haveToPaginate(); // whether the number of results if higher than the max per page
-        
         return array($tags, $num_page, $nb_by_page, $searched, $sort, $reverse_sort, $pagerfanta);
     }
 
--- a/Model/BaseDocumentInterface.php	Tue Oct 25 12:20:47 2011 +0200
+++ b/Model/BaseDocumentInterface.php	Wed Oct 26 15:38:05 2011 +0200
@@ -40,6 +40,12 @@
      */
     function getManualOrder();
     
+    /**
+     *  Get Tags
+     *
+     */
+    function getTags();
+    
     
     
 }
--- a/Model/Document.php	Tue Oct 25 12:20:47 2011 +0200
+++ b/Model/Document.php	Wed Oct 26 15:38:05 2011 +0200
@@ -10,6 +10,8 @@
 
 namespace IRI\Bundle\WikiTagBundle\Model;
 
+use Doctrine\Common\Collections\ArrayCollection;
+
 abstract class Document implements BaseDocumentInterface {
     
     /**
@@ -37,6 +39,11 @@
      */
     protected $externalId;
     
+    /**
+     * @var ArrayCollection $tags
+     */
+    protected $tags;
+    
     
     /**
      * Get id
@@ -126,4 +133,13 @@
         return $this->externalId;
     }
     
+    /**
+     * TODO: (non-PHPdoc)
+     * @see IRI\Bundle\WikiTagBundle\Model.BaseDocumentInterface::getTags()
+     */
+    function getTags()
+    {
+        return $this->tags;
+    }
+    
 }
--- a/Model/DocumentInterface.php	Tue Oct 25 12:20:47 2011 +0200
+++ b/Model/DocumentInterface.php	Wed Oct 26 15:38:05 2011 +0200
@@ -11,6 +11,8 @@
 
 namespace IRI\Bundle\WikiTagBundle\Model;
 
+use Doctrine\Common\Collections\ArrayCollection;
+
 interface DocumentInterface {
     
     /**
--- a/Model/Tag.php	Tue Oct 25 12:20:47 2011 +0200
+++ b/Model/Tag.php	Wed Oct 26 15:38:05 2011 +0200
@@ -9,7 +9,9 @@
  */
  namespace IRI\Bundle\WikiTagBundle\Model;
  
- use IRI\Bundle\WikiTagBundle\Utils\WikiTagUtils;
+ use Doctrine\Common\Collections\ArrayCollection;
+
+use IRI\Bundle\WikiTagBundle\Utils\WikiTagUtils;
 
 abstract class Tag implements TagInterface {
     
@@ -70,6 +72,11 @@
      */
     protected $category;
 
+    /**
+     * @var ArrayCollection $documents
+     */
+    protected $documents;
+
 
     /**
      * Get id
@@ -301,6 +308,16 @@
     {
         return $this->category;
     }
+
+    /**
+     * Get Documents
+     *
+     * @return ArrayCollection
+     */
+    public function getDocuments()
+    {
+        return $this->documents;
+    }
     
     /**
      * Set category to Null
--- a/Model/TagInterface.php	Tue Oct 25 12:20:47 2011 +0200
+++ b/Model/TagInterface.php	Wed Oct 26 15:38:05 2011 +0200
@@ -11,6 +11,8 @@
 namespace IRI\Bundle\WikiTagBundle\Model;
 
 
+use Doctrine\Common\Collections\ArrayCollection;
+
 interface TagInterface {
     
     /**
@@ -169,5 +171,18 @@
      */
     function getCategory();
     
+    /**
+     * Get Documents
+     *
+     * @return ArrayCollection
+     */
+    function getDocuments();
+    
+    /**
+     * Nullify category
+     *
+     */
+    function nullCategory();
+    
     
 }
\ No newline at end of file
--- a/Resources/config/doctrine/Document.orm.yml	Tue Oct 25 12:20:47 2011 +0200
+++ b/Resources/config/doctrine/Document.orm.yml	Wed Oct 26 15:38:05 2011 +0200
@@ -19,4 +19,8 @@
     externalId:
       type: string
       length: '1024'
+  oneToMany:
+    tags:
+      targetEntity: DocumentTag
+      mappedBy: document
   lifecycleCallbacks: {  }
--- a/Resources/config/doctrine/DocumentTag.orm.yml	Tue Oct 25 12:20:47 2011 +0200
+++ b/Resources/config/doctrine/DocumentTag.orm.yml	Wed Oct 26 15:38:05 2011 +0200
@@ -23,6 +23,7 @@
   manyToOne:
     tag:
       targetEntity: Tag
+      inversedBy: documents
       joinColumn:
         name: tag_id
         referencedColumnName: id
@@ -30,6 +31,7 @@
         onDelete: CASCADE
     document:
       targetEntity: Document
+      inversedBy: tags
       joinColumn: 
         name: document_id
         referencedColumnName: id
--- a/Resources/config/doctrine/Tag.orm.yml	Tue Oct 25 12:20:47 2011 +0200
+++ b/Resources/config/doctrine/Tag.orm.yml	Wed Oct 26 15:38:05 2011 +0200
@@ -46,4 +46,8 @@
   manyToOne:
     category:
       targetEntity: Category
+  oneToMany:
+    documents:
+      targetEntity: DocumentTag
+      mappedBy: tag
   lifecycleCallbacks: {  }
--- a/Resources/views/WikiTag/TagListTable.html.twig	Tue Oct 25 12:20:47 2011 +0200
+++ b/Resources/views/WikiTag/TagListTable.html.twig	Wed Oct 26 15:38:05 2011 +0200
@@ -52,7 +52,8 @@
             </a>
         {% endif %}
         </th></tr>
-    {% for tag in tags %}
+    {% for ar in tags %}
+    {% set tag, nb_docs = ar.0, ar.nb_docs %}
     <tr class="imageline {{ cycle(['wikitag_oddline', 'wikitag_evenline'], loop.index) }}">
         <td class="wikitag_reset_wp_info">{{tag.id}}</td>
         <td class="wikitag_{{tag.urlstatustext}} wikipediatag" id="{{tag.id}}" >{{tag.label}}</td>
@@ -75,12 +76,12 @@
         <td class="wikitag_text_centered"><img src="{{ asset('bundles/wikitag/images/red_cross.png') }}" class="wikitag_remove_wp_link" id="{{tag.id}}" alt="{{tag.label}}" /></td>
         <td class="wikitag_alias" id="{{tag.id}}" >{% if tag.alias %}{{tag.alias}}{% endif %}</td>
         <td class="wikitag_text_centered">
-        ?
+        {{nb_docs}}
 {#
-        {% if tag.num_ds > 0 %}
-            <a href="{% url hdabo.views.display_datasheet %}?tag={{tag.id}}" >{{tag.num_ds}}</a>
+        {% if nb_docs > 0 %}
+            <a href="{{ url('document_with_tags', { 'idDoc': '9' }) }}?tag={{tag.id}}" >{{nb_docs}}</a>
         {% else %}
-            {{tag.num_ds}}
+            {{nb_docs}}
         {% endif %}
 #}
         </td>