# HG changeset patch # User cavaliet # Date 1319636285 -7200 # Node ID 673b2766024ea56afad92ed8b12c5032ea3799d5 # Parent c288952a089f616e36d9fc78f8335f2d2f0ab756 Update ORM configuration to allow JOIN between Tag and DocumentTag. TagList template has now the number of documents by tag. diff -r c288952a089f -r 673b2766024e Controller/WikiTagController.php --- 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); } diff -r c288952a089f -r 673b2766024e Model/BaseDocumentInterface.php --- 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(); + } diff -r c288952a089f -r 673b2766024e Model/Document.php --- 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; + } + } diff -r c288952a089f -r 673b2766024e Model/DocumentInterface.php --- 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 { /** diff -r c288952a089f -r 673b2766024e Model/Tag.php --- 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 diff -r c288952a089f -r 673b2766024e Model/TagInterface.php --- 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 diff -r c288952a089f -r 673b2766024e Resources/config/doctrine/Document.orm.yml --- 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: { } diff -r c288952a089f -r 673b2766024e Resources/config/doctrine/DocumentTag.orm.yml --- 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 diff -r c288952a089f -r 673b2766024e Resources/config/doctrine/Tag.orm.yml --- 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: { } diff -r c288952a089f -r 673b2766024e Resources/views/WikiTag/TagListTable.html.twig --- 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 @@ {% endif %} - {% for tag in tags %} + {% for ar in tags %} + {% set tag, nb_docs = ar.0, ar.nb_docs %}
 }})