bastien 13 years ago
parent
commit
ec956482e7
1 changed files with 45 additions and 7 deletions
  1. 45 7
      src/Muzich/CoreBundle/Repository/ElementRepository.php

+ 45 - 7
src/Muzich/CoreBundle/Repository/ElementRepository.php View File

@@ -71,12 +71,6 @@ class ElementRepository extends EntityRepository
71 71
     $join_tags  = '';
72 72
     if (count(($tags = $searcher->getTags())))
73 73
     {
74
-      // Recherche strict ou non ?
75
-      $strict_word = 'OR';
76
-      if ($searcher->getTagStrict())
77
-      {
78
-        $strict_word = 'AND';
79
-      }
80 74
       
81 75
       foreach ($tags as $tag_id => $tag_name)
82 76
       {
@@ -90,7 +84,7 @@ class ElementRepository extends EntityRepository
90 84
         }
91 85
         else
92 86
         {
93
-          $where_tags .= ' '.$strict_word.' t_.id = :tid'.$tag_id;
87
+          $where_tags .= ' OR t_.id = :tid'.$tag_id;
94 88
         }
95 89
         $params_ids['tid'.$tag_id] = $tag_id;
96 90
       }
@@ -191,6 +185,49 @@ class ElementRepository extends EntityRepository
191 185
       $order_by = "ORDER BY e_.created ASC, e_.id ASC";
192 186
     }
193 187
     
188
+    
189
+    // Recherche strict ou non ?
190
+    $where_tag_strict = '';
191
+    if ($searcher->getTagStrict() && count($tags))
192
+    {
193
+      // On a besoin de récupérer la liste des element_id qui ont les tags
194
+      // demandés.
195
+      $tag_ids = '';
196
+      foreach ($tags as $tag_id => $tag_name)
197
+      {
198
+        if ($tag_ids === '')
199
+        {
200
+          $tag_ids .= (int)$tag_id;
201
+        }
202
+        else
203
+        {
204
+          $tag_ids .= ','.(int)$tag_id;
205
+        }
206
+      }
207
+      
208
+      $sql = "SELECT et.element_id FROM elements_tag et "
209
+      ."WHERE et.tag_id IN ($tag_ids) group by et.element_id "
210
+      ."having count (distinct et.tag_id) = ".count($tags);
211
+      $rsm = new \Doctrine\ORM\Query\ResultSetMapping;
212
+      $rsm->addScalarResult('element_id', 'element_id');
213
+      
214
+      $strict_element_ids_result = $this->getEntityManager()
215
+        ->createNativeQuery($sql, $rsm)
216
+        //->setParameter('ids', $tag_ids)
217
+        ->getScalarResult()
218
+      ;
219
+      
220
+      $strict_element_ids = array();
221
+      foreach ($strict_element_ids_result as $strict_id)
222
+      {
223
+        $strict_element_ids[] = $strict_id['element_id'];
224
+      }
225
+      
226
+      $where_tag_strict = ($is_where) ? ' AND' : ' WHERE';
227
+      $where_tag_strict .= ' e_.id IN (:tag_strict_ids)';
228
+      $params_ids['tag_strict_ids'] = $strict_element_ids;
229
+    }
230
+    
194 231
     // Requête qui selectionnera les ids en fonction des critéres
195 232
     $id_query = $this->getEntityManager()
196 233
       ->createQuery(
@@ -205,6 +242,7 @@ class ElementRepository extends EntityRepository
205 242
         $where_group
206 243
         $where_favorite
207 244
         $where_id_limit
245
+        $where_tag_strict
208 246
         GROUP BY e_.id
209 247
         $order_by")
210 248
      ->setParameters($params_ids)