[limb-svn] r6766 - in 3.x/trunk/limb/active_record: src tests/cases

svn at limb-project.com svn at limb-project.com
Wed Feb 6 15:06:00 MSK 2008


Author: serega
Date: 2008-02-06 15:06:00 +0300 (Wed, 06 Feb 2008)
New Revision: 6766
URL: http://fisheye.limb-project.com/changelog/limb/?cs=6766

Modified:
   3.x/trunk/limb/active_record/src/lmbARRecordSetAttachDecorator.class.php
   3.x/trunk/limb/active_record/tests/cases/lmbARQueryTest.class.php
Log:
-- fixed lmbARRecordSetAttachDecorator: now it does not make a query in case of empty decorated iterator

Modified: 3.x/trunk/limb/active_record/src/lmbARRecordSetAttachDecorator.class.php
===================================================================
--- 3.x/trunk/limb/active_record/src/lmbARRecordSetAttachDecorator.class.php	2008-01-31 10:19:00 UTC (rev 6765)
+++ 3.x/trunk/limb/active_record/src/lmbARRecordSetAttachDecorator.class.php	2008-02-06 12:06:00 UTC (rev 6766)
@@ -51,20 +51,30 @@
         case lmbActiveRecord :: HAS_ONE:
         case lmbActiveRecord :: MANY_BELONGS_TO:
           $ids = lmbArrayHelper :: getColumnValues($this->prefix . $relation_info['field'], $this->iterator);
-          $attached_objects = lmbActiveRecord :: findByIds($relation_class, $ids, $params, $this->conn);
-          $this->loaded_attaches[$relation_name] = lmbCollection :: toFlatArray($attached_objects, 
-                                                                                $key_field = $relation_object->getPrimaryKeyName(),
-                                                                                $export_each = false); 
+          if(!count($ids))
+            $this->loaded_attaches[$relation_name] = array();
+          else
+          {
+            $attached_objects = lmbActiveRecord :: findByIds($relation_class, $ids, $params, $this->conn);
+            $this->loaded_attaches[$relation_name] = lmbCollection :: toFlatArray($attached_objects, 
+                                                                                  $key_field = $relation_object->getPrimaryKeyName(),
+                                                                                  $export_each = false);
+          }
         break;
         case lmbActiveRecord :: BELONGS_TO:
           $ids = lmbArrayHelper :: getColumnValues($this->prefix . $this->base_object->getPrimaryKeyName(), $this->iterator);
           
-          $criteria = lmbSQLCriteria :: in($relation_info['field'], $ids);
-          $params['criteria'] = isset($params['criteria']) ? $params['criteria']->addAnd($criteria) : $criteria;
-          $attached_objects = lmbActiveRecord :: find($relation_class, $params, $this->conn);
-          $this->loaded_attaches[$relation_name] = lmbCollection :: toFlatArray($attached_objects, 
-                                                                                $key_field = $relation_info['field'], 
-                                                                                $export_each = false); 
+          if(!count($ids))
+            $this->loaded_attaches[$relation_name] = array();
+          else
+          {
+            $criteria = lmbSQLCriteria :: in($relation_info['field'], $ids);
+            $params['criteria'] = isset($params['criteria']) ? $params['criteria']->addAnd($criteria) : $criteria;
+            $attached_objects = lmbActiveRecord :: find($relation_class, $params, $this->conn);
+            $this->loaded_attaches[$relation_name] = lmbCollection :: toFlatArray($attached_objects, 
+                                                                                  $key_field = $relation_info['field'], 
+                                                                                  $export_each = false);
+          }
         break;
         case lmbActiveRecord :: HAS_MANY:
           if(!isset($params['sort']))
@@ -75,12 +85,18 @@
           $query = lmbAROneToManyCollection :: createFullARQueryForRelation($relation_info, $this->conn, $params);
           
           $ids = lmbArrayHelper :: getColumnValues($this->prefix . $this->base_object->getPrimaryKeyName(), $this->iterator);
-          $query->addCriteria(lmbSQLCriteria :: in($relation_info['field'], $ids));
           
-          $attached_objects = $query->fetch();
-          
-          foreach($attached_objects as $attached_object)
-            $this->loaded_attaches[$relation_name][$attached_object->get($relation_info['field'])][] = $attached_object; 
+          if(!count($ids))
+            $this->loaded_attaches[$relation_name] = array();
+          else
+          {
+            $query->addCriteria(lmbSQLCriteria :: in($relation_info['field'], $ids));
+            
+            $attached_objects = $query->fetch();
+            
+            foreach($attached_objects as $attached_object)
+              $this->loaded_attaches[$relation_name][$attached_object->get($relation_info['field'])][] = $attached_object;
+          }
         break;
         case lmbActiveRecord :: HAS_MANY_TO_MANY:
           if(!isset($params['sort']))
@@ -92,12 +108,17 @@
           $query->addField($relation_info['table']. '.' . $relation_info['field'], "link__id");
           
           $ids = lmbArrayHelper :: getColumnValues($this->prefix . $this->base_object->getPrimaryKeyName(), $this->iterator);
-          $query->addCriteria(lmbSQLCriteria :: in($relation_info['field'], $ids));
-          
-          $attached_objects = $query->fetch();
-          
-          foreach($attached_objects as $attached_object)
-            $this->loaded_attaches[$relation_name][$attached_object->get("link__id")][] = $attached_object; 
+          if(!count($ids))
+            $this->loaded_attaches[$relation_name] = array();
+          else
+          {          
+            $query->addCriteria(lmbSQLCriteria :: in($relation_info['field'], $ids));
+            
+            $attached_objects = $query->fetch();
+            
+            foreach($attached_objects as $attached_object)
+              $this->loaded_attaches[$relation_name][$attached_object->get("link__id")][] = $attached_object;
+          }
         break;
       }
     }

Modified: 3.x/trunk/limb/active_record/tests/cases/lmbARQueryTest.class.php
===================================================================
--- 3.x/trunk/limb/active_record/tests/cases/lmbARQueryTest.class.php	2008-01-31 10:19:00 UTC (rev 6765)
+++ 3.x/trunk/limb/active_record/tests/cases/lmbARQueryTest.class.php	2008-02-06 12:06:00 UTC (rev 6766)
@@ -78,7 +78,7 @@
 
     $this->assertEqual($this->conn->countQueries(), 0);
   }
-
+  
   function testFetch_Join_RelatedBelongsToObject()
   {
     $person1 = $this->creator->createPerson();
@@ -161,7 +161,7 @@
     $this->conn->resetStats();
     
     $query = lmbARQuery :: create('PersonForTest', array(), $this->conn);
-    // note attach() has the same effect as join() but workds is a different way - it produces another sql request 
+    // note attach() has the same effect as join() but works in a different way - it produces another sql request 
     $iterator = $query->eagerAttach('social_security')->fetch();
     $arr = $iterator->getArray();
     
@@ -184,6 +184,32 @@
 
     $this->assertEqual($this->conn->countQueries(), 0);
   }
+  
+  function testFetch_Attach_WhenEmptyRecordSet_ForHasOneRelation()
+  {
+    $this->conn->resetStats();
+    
+    $query = lmbARQuery :: create('PersonForTest', array(), $this->conn);
+    // note attach() has the same effect as join() but workds is a different way - it produces another sql request 
+    $iterator = $query->eagerAttach('social_security')->fetch();
+    $arr = $iterator->getArray();
+    $this->assertEqual(sizeof($arr), 0);
+    
+    $this->assertEqual($this->conn->countQueries(), 1);
+  }
+  
+  function testFetch_Attach_WhenEmptyRecordSet_ForBelongsToRelation()
+  {
+    $this->conn->resetStats();
+    
+    $query = lmbARQuery :: create('SocialSecurityForTest', array(), $this->conn);
+    // note attach() has the same effect as join() but workds is a different way - it produces another sql request 
+    $iterator = $query->eagerAttach('person')->fetch();
+    $arr = $iterator->getArray();
+    $this->assertEqual(sizeof($arr), 0);
+    
+    $this->assertEqual($this->conn->countQueries(), 1);
+  }  
 
   function testFetch_Attach_RelatedBelongsToObjects()
   {
@@ -309,6 +335,16 @@
     
     $this->assertEqual($this->conn->countQueries(), 0);
   }
+  
+  function testFetch_Attach_WithEmptyRS_ForRelatedHasMany()
+  {
+    $this->conn->resetStats();
+    
+    $query = lmbARQuery :: create('CourseForTest', array(), $this->conn);
+    $arr = $query->eagerAttach('lectures')->fetch()->getArray();
+    
+    $this->assertEqual($this->conn->countQueries(), 1);
+  }
 
   function testFetch_Attach_RelatedHasManyToMany()
   {



More information about the limb-svn mailing list