ソースを参照

Optimisation de al requete de mise a jours des tags favoris.

bastien 12 年 前
コミット
ce293f284b
共有2 個のファイルを変更した32 個の追加21 個の削除を含む
  1. 18 21
      src/Muzich/CoreBundle/Entity/User.php
  2. 14 0
      src/Muzich/CoreBundle/Repository/TagRepository.php

+ 18 - 21
src/Muzich/CoreBundle/Entity/User.php ファイルの表示

@@ -348,13 +348,10 @@ class User extends BaseUser
348 348
    */
349 349
   public function updateTagsFavoritesById(EntityManager $em, $ids)
350 350
   {
351
-    // TODO: attention aux relations déjà existantes.
352
-    // TODO penser a supprimer celles qui n'existes plus.
353
-    
354 351
     $ids_to_add = $ids;
355 352
     
356 353
     // Pour chacun des tags favoris 
357
-    foreach ($this->tags_favorites as $tag_favorite)
354
+    foreach ($this->tags_favorites as $ii => $tag_favorite)
358 355
     {
359 356
       $trouve = false;
360 357
       foreach ($ids as $i => $id)
@@ -365,6 +362,7 @@ class User extends BaseUser
365 362
           // Si le tag était favoris déjà avant (et aussi maintenant)
366 363
           // il ne sera ni a ajouter, ni a supprimer.
367 364
           unset($ids_to_add[$i]);
365
+          var_dump($ids_to_add);
368 366
         }
369 367
       }
370 368
       
@@ -376,27 +374,26 @@ class User extends BaseUser
376 374
       }
377 375
     }
378 376
     
379
-    $tag_favorite_position_max = $this->getTagFavoritePositionMax();
380
-    
381
-    // Pour les nouveaux ids restants
382
-    foreach ($ids as $id)
377
+    if (count($ids_to_add))
383 378
     {
384
-      $tag = $em->getRepository('MuzichCoreBundle:Tag')
385
-        ->findOneById($id)
386
-      ;
387
-      
388
-      $tag_favorite = new UsersTagsFavorites();
389
-      $tag_favorite->setUser($this);
390
-      $tag_favorite->setTag($tag);
391
-      $tag_favorite->setPosition($tag_favorite_position_max);
392
-      $tag_favorite_position_max++;
393
-      
394
-      $this->addUsersTagsFavorites($tag_favorite);
395
-      $em->persist($tag_favorite);
379
+      $tag_favorite_position_max = $this->getTagFavoritePositionMax();
380
+      $tags = $em->getRepository('MuzichCoreBundle:Tag')->findByIds($ids_to_add)->execute();
381
+
382
+      // Pour les nouveaux ids restants
383
+      foreach ($tags as $tag)
384
+      {      
385
+        $tag_favorite = new UsersTagsFavorites();
386
+        $tag_favorite->setUser($this);
387
+        $tag_favorite->setTag($tag);
388
+        $tag_favorite->setPosition($tag_favorite_position_max);
389
+        $tag_favorite_position_max++;
390
+
391
+        $this->addUsersTagsFavorites($tag_favorite);
392
+        $em->persist($tag_favorite);
393
+      }
396 394
     }
397 395
     
398 396
     $em->flush();
399
-    
400 397
   }
401 398
   
402 399
   /**

+ 14 - 0
src/Muzich/CoreBundle/Repository/TagRepository.php ファイルの表示

@@ -27,5 +27,19 @@ class TagRepository extends EntityRepository
27 27
     return $tag_array;
28 28
   }
29 29
   
30
+  /**
31
+   * Retourne une Query selectionnant des tags pour leurs id
32
+   * 
33
+   * @param array $ids
34
+   * @return \Doctrine\ORM\Query 
35
+   */
36
+  public function findByIds($ids)
37
+  {
38
+    return $this->getEntityManager()->createQuery("
39
+        SELECT t FROM MuzichCoreBundle:Tag t
40
+        WHERE t.id IN (:tids)
41
+    ")->setParameter('tids', $ids);
42
+  }
43
+  
30 44
 }
31 45