Skip to content
This repository
Browse code

AbstractQuery: Now we can return result of $query->execute() in PRE_Q…

…UERY_EXECUTE event
  • Loading branch information...
commit ee20e266857689f288ee64ac3b2167ee5990cdcd 1 parent e494e8a
Semyon authored July 16, 2012
5  Event/IfMaxResultsEqualOneSubscriber.php → Event/GetSingleResultSubscriber.php
@@ -8,7 +8,7 @@
8 8
 /**
9 9
  * @author Semyon Velichko <semyon@velichko.net>
10 10
  */
11  
-class IfMaxResultsEqualOneSubscriber implements EventSubscriberInterface
  11
+class GetSingleResultSubscriber implements EventSubscriberInterface
12 12
 {
13 13
 
14 14
     public static function getSubscribedEvents()
@@ -20,8 +20,7 @@ public function callbackFunction(ResponseEvent $event)
20 20
     {
21 21
         $data = $event->getData();
22 22
         
23  
-        if(1 === $data->getMaxResults())
24  
-            $data->setHydrationMode(ORMAbstractQuery::HYDRATE_SINGLE_SCALAR);
  23
+        $data = $data->setMaxResults(1)->getSingleResult(ORMAbstractQuery::HYDRATE_ARRAY);
25 24
         
26 25
         $event->setData($data);
27 26
     }
11  Response/AbstractQuery.php
@@ -18,6 +18,7 @@ class AbstractQuery extends Response implements ResponseInterface
18 18
 {
19 19
     
20 20
     protected $query;
  21
+    protected $singleAsRoot = false;
21 22
     
22 23
     public function setContent($query)
23 24
     {
@@ -34,13 +35,19 @@ public function extract()
34 35
             ->setHydrationMode(ORMAbstractQuery::HYDRATE_ARRAY);
35 36
             
36 37
         $event = new ResponseEvent($this, $data);
37  
-        $data = $this->dispatcher->dispatch(DirectEvents::PRE_QUERY_EXECUTE, $event);
  38
+        $this->dispatcher->dispatch(DirectEvents::PRE_QUERY_EXECUTE, $event);
38 39
         
39  
-        $data = $data->execute();
  40
+        $data = $event->getData();
  41
+        if(!is_array($data) && ($data instanceof ORMAbstractQuery))
  42
+            $data = $data->execute();
40 43
         
41 44
         $event = new ResponseEvent($this, $data);
42 45
         $this->dispatcher->dispatch(DirectEvents::POST_QUERY_EXECUTE, $event);
43 46
         $data = $event->getData();
  47
+        
  48
+        if(!is_array($data))
  49
+            throw new \InvalidArgumentException('Final result should be an array');
  50
+            
44 51
         return $this->formatResponse($data);
45 52
     }
46 53
 }

0 notes on commit ee20e26

Please sign in to comment.
Something went wrong with that request. Please try again.