Browse Source

Mise en place et test de l'objet de paramétrage de la recherche d'element, et sa requete (debut).

bastien 13 years ago
parent
commit
e4a586b46d

+ 1 - 0
src/Muzich/CoreBundle/Entity/Element.php View File

@@ -12,6 +12,7 @@ use \Doctrine\Common\Collections\ArrayCollection;
12 12
  * 
13 13
  * @ORM\Entity
14 14
  * @ORM\Table(name="element")
15
+ * @ORM\Entity(repositoryClass="Muzich\CoreBundle\Repository\ElementRepository")
15 16
  */
16 17
 class Element
17 18
 {

+ 58 - 8
src/Muzich/CoreBundle/Repository/ElementRepository.php View File

@@ -3,6 +3,7 @@
3 3
 namespace Muzich\CoreBundle\Repository;
4 4
 
5 5
 use Doctrine\ORM\EntityRepository;
6
+use Muzich\CoreBundle\Searcher\ElementSearcher;
6 7
 
7 8
 class ElementRepository extends EntityRepository
8 9
 {
@@ -13,14 +14,63 @@ class ElementRepository extends EntityRepository
13 14
    * @return array 
14 15
    */
15 16
   public function findAllOrderedByName()
17
+  {
18
+    return $this->getEntityManager()
19
+      ->createQuery('
20
+        SELECT e, t FROM MuzichCoreBundle:Element e 
21
+        JOIN e.type t
22
+        ORDER BY e.name ASC'
23
+      )
24
+      ->getResult()
25
+    ;
26
+  }
27
+  
28
+  /**
29
+   *
30
+   * @param ElementSearcher $searcher
31
+   * @return Doctrine\ORM\Query
32
+   */
33
+  public function findBySearch(ElementSearcher $searcher)
34
+  {
35
+    $query_select = "e, et, t";
36
+    $query_join = "e.type et JOIN e.tags t";
37
+    $query_where = "";
38
+    $query_with = "WITH ";
39
+    $params = array();
40
+    
41
+    switch ($searcher->getNetwork())
16 42
     {
17
-        return $this->getEntityManager()
18
-          ->createQuery('
19
-            SELECT e, t FROM MuzichCoreBundle:Element e 
20
-            JOIN e.type t
21
-            ORDER BY e.name ASC'
22
-          )
23
-          ->getResult()
24
-        ;
43
+      case ElementSearcher::NETWORK_PUBLIC:
44
+        
45
+      break;
46
+    
47
+      case ElementSearcher::NETWORK_PERSONAL:
48
+        
49
+      break;
25 50
     }
51
+    
52
+    foreach ($searcher->getTags() as $tag)
53
+    {
54
+      if ($query_with != "WITH ")
55
+      {
56
+        $query_with .= "OR ";
57
+      }
58
+      $query_with .= "t.id = :tagid".$tag->getId()." ";
59
+      $params['tagid'.$tag->getId()] = $tag->getId();
60
+    }
61
+    
62
+    $query_string = "SELECT $query_select 
63
+      FROM MuzichCoreBundle:Element e 
64
+      JOIN $query_join $query_with
65
+      ORDER BY e.date_added DESC"
66
+    ;
67
+    
68
+    $query = $this->getEntityManager()
69
+      ->createQuery($query_string)
70
+      ->setParameters($params)
71
+    ;
72
+    
73
+    return $query;
74
+  }
75
+  
26 76
 }

+ 51 - 36
src/Muzich/CoreBundle/Searcher/ElementSearcher.php View File

@@ -34,19 +34,19 @@ class ElementSearcher extends Searcher implements SearcherInterface
34 34
    */
35 35
   protected $count = 20;
36 36
   
37
-  /**
38
-   * Objet requete
39
-   * 
40
-   * @var  
41
-   */
42
-  protected $query = null;
43
-  
44
-  /**
45
-   * Liste des Element Résultats
46
-   * 
47
-   * @var array
48
-   */
49
-  protected $results = array();
37
+//  /**
38
+//   * Objet requete
39
+//   * 
40
+//   * @var  
41
+//   */
42
+//  protected $query = null;
43
+//  
44
+//  /**
45
+//   * Liste des Element Résultats
46
+//   * 
47
+//   * @var array
48
+//   */
49
+//  protected $results = array();
50 50
   
51 51
   /**
52 52
    * @see SearcherInterface
@@ -78,36 +78,51 @@ class ElementSearcher extends Searcher implements SearcherInterface
78 78
     ), $params);
79 79
   }
80 80
   
81
-  /**
82
-   * @see SearcherInterface
83
-   */
84
-  public function constructQueryObject()
81
+//  /**
82
+//   * @see SearcherInterface
83
+//   */
84
+//  public function constructQueryObject()
85
+//  {
86
+//    
87
+//  }
88
+//  
89
+//  /**
90
+//   * @see SearcherInterface
91
+//   * @return 
92
+//   */
93
+//  public function getQueryObject()
94
+//  {
95
+//    if (!$this->query)
96
+//    {
97
+//      $this->constructQueryObject();
98
+//    }
99
+//    
100
+//    return $this->query;
101
+//  }
102
+//  
103
+//  public function getResults()
104
+//  {
105
+//    if (!$this->query)
106
+//    {
107
+//      $this->constructQueryObject();
108
+//    }
109
+//    
110
+//    //...
111
+//  }
112
+  
113
+  public function getNetwork()
85 114
   {
86
-    
115
+    return $this->network;
87 116
   }
88 117
   
89
-  /**
90
-   * @see SearcherInterface
91
-   * @return 
92
-   */
93
-  public function getQueryObject()
118
+  public function getTags()
94 119
   {
95
-    if (!$this->query)
96
-    {
97
-      $this->constructQueryObject();
98
-    }
99
-    
100
-    return $this->query;
120
+    return $this->tags;
101 121
   }
102 122
   
103
-  public function getResults()
123
+  public function getCount()
104 124
   {
105
-    if (!$this->query)
106
-    {
107
-      $this->constructQueryObject();
108
-    }
109
-    
110
-    //...
125
+    return $this->count;
111 126
   }
112 127
   
113 128
 }

+ 4 - 0
src/Muzich/CoreBundle/Searcher/Searcher.php View File

@@ -16,6 +16,10 @@ class Searcher
16 16
       {
17 17
         throw new \Exception($message);
18 18
       }
19
+      elseif (empty($params[$config_id]))
20
+      {
21
+        throw new \Exception($message);
22
+      }
19 23
     }
20 24
   }
21 25
   

+ 15 - 15
src/Muzich/CoreBundle/Searcher/SearcherInterface.php View File

@@ -19,20 +19,20 @@ interface SearcherInterface
19 19
    */
20 20
   public function update($params);
21 21
   
22
-  /**
23
-   * Procédure qui construit la requete.
24
-   */
25
-  public function constructQueryObject();
26
-  
27
-  /**
28
-   * Récupération de l'objet requete, pour ajouter des JOIN 
29
-   * par exemple.
30
-   */
31
-  public function getQueryObject();
32
-  
33
-  /**
34
-   * Récupération des résultats.
35
-   */
36
-  public function getResults();
22
+//  /**
23
+//   * Procédure qui construit la requete.
24
+//   */
25
+//  public function constructQueryObject();
26
+//  
27
+//  /**
28
+//   * Récupération de l'objet requete, pour ajouter des JOIN 
29
+//   * par exemple.
30
+//   */
31
+//  public function getQueryObject();
32
+//  
33
+//  /**
34
+//   * Récupération des résultats.
35
+//   */
36
+//  public function getResults();
37 37
   
38 38
 }

+ 17 - 5
src/Muzich/HomeBundle/Controller/HomeController.php View File

@@ -7,6 +7,7 @@ use Symfony\Bundle\FrameworkBundle\Controller\Controller;
7 7
 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
8 8
 
9 9
 use Muzich\CoreBundle\Searcher\ElementSearcher;
10
+use Doctrine\ORM\Query;
10 11
 
11 12
 class HomeController extends Controller
12 13
 {
@@ -17,15 +18,26 @@ class HomeController extends Controller
17 18
   public function indexAction()
18 19
   {        
19 20
     $user = $this->container->get('security.context')->getToken()->getUser();
20
-    $s = new ElementSearcher();
21
-    $s->init(array(
21
+    
22
+    $search = new ElementSearcher();
23
+    
24
+    $search->init(array(
22 25
       'network' => ElementSearcher::NETWORK_PUBLIC,
23
-      'tags' => array('toto', 'pipi'),
26
+      'tags' => array(
27
+        $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Hardtek'),
28
+        $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Tribe')
29
+      ),
24 30
       'count' => 30
25 31
     ));
26 32
     
27
-    //die(var_dump($s));
33
+    $query = $this->getDoctrine()
34
+      ->getRepository('MuzichCoreBundle:Element')
35
+      ->findBySearch($search)
36
+    ;
37
+    
38
+    //$product = new Query();
39
+//    $product->execute();
28 40
     
29
-    return array('user' => $user);
41
+    return array('elements' => $query->execute());
30 42
   }
31 43
 }

+ 16 - 1
src/Muzich/HomeBundle/Resources/views/Home/index.html.twig View File

@@ -4,7 +4,22 @@
4 4
 
5 5
 {% block content %}
6 6
     
7
-  Salut {{ user.username }}
7
+<ul>
8
+  {% for element in elements %} 
9
+    <li>
10
+      {{element.name}}
11
+        
12
+        <ul>
13
+          {% for tag in element.tags %} 
14
+            <li>
15
+              {{tag.name}}
16
+            </li>
17
+          {% endfor %} 
18
+        </ul>
19
+        
20
+    </li>
21
+  {% endfor %} 
22
+</ul>
8 23
     
9 24
     
10 25
 {% endblock %}