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

svn at limb-project.com svn at limb-project.com
Mon Jan 21 10:52:52 MSK 2008


Author: serega
Date: 2008-01-21 10:52:52 +0300 (Mon, 21 Jan 2008)
New Revision: 6710
URL: http://fisheye.limb-project.com/changelog/limb/?cs=6710

Modified:
   3.x/trunk/limb/active_record/src/lmbARQuery.class.php
   3.x/trunk/limb/active_record/src/lmbARRecordSetAttachDecorator.class.php
   3.x/trunk/limb/active_record/tests/cases/lmbARQueryTest.class.php
Log:
-- fixed lmbARQuery for case when join() is used with wrong relation type (has_many or has_many_to_many). Now it throws an exception.
-- several other minor fixes related eager fetching functionality

Modified: 3.x/trunk/limb/active_record/src/lmbARQuery.class.php
===================================================================
--- 3.x/trunk/limb/active_record/src/lmbARQuery.class.php	2008-01-20 19:06:51 UTC (rev 6709)
+++ 3.x/trunk/limb/active_record/src/lmbARQuery.class.php	2008-01-21 07:52:52 UTC (rev 6710)
@@ -110,6 +110,9 @@
                              $base_object->getPrimaryKeyName(),
                              $prefix . $relation_name);
         break;
+        default:
+           throw new lmbARException('"' . $relation_name . '" has a wrong relation type for JOIN operation');
+        break;
       }
       
       if(isset($params['join']))

Modified: 3.x/trunk/limb/active_record/src/lmbARRecordSetAttachDecorator.class.php
===================================================================
--- 3.x/trunk/limb/active_record/src/lmbARRecordSetAttachDecorator.class.php	2008-01-20 19:06:51 UTC (rev 6709)
+++ 3.x/trunk/limb/active_record/src/lmbARRecordSetAttachDecorator.class.php	2008-01-21 07:52:52 UTC (rev 6710)
@@ -22,6 +22,7 @@
   protected $conn;
   protected $attach_relations = array();
   protected $prefix = "";
+  protected $loaded_attaches = array();
 
   function __construct($record_set, $base_object, $conn = null, $attach_relations = array(), $prefix = "")
   {
@@ -119,16 +120,21 @@
       {
         case lmbActiveRecord :: HAS_ONE:
         case lmbActiveRecord :: MANY_BELONGS_TO:
-          $fields->set($this->prefix . $relation_name, $this->loaded_attaches[$relation_name][$object->get($this->prefix . $relation_info['field'])]);
+          if(isset($this->loaded_attaches[$relation_name][$object->get($this->prefix . $relation_info['field'])]))
+            $fields->set($this->prefix . $relation_name, $this->loaded_attaches[$relation_name][$object->get($this->prefix . $relation_info['field'])]);
         break;
         case lmbActiveRecord :: BELONGS_TO:
-          $fields->set($this->prefix . $relation_name, $this->loaded_attaches[$relation_name][$object->get($this->prefix . $this->base_object->getPrimaryKeyName())]);
+          if(isset($this->loaded_attaches[$relation_name][$object->get($this->prefix . $this->base_object->getPrimaryKeyName())]))
+            $fields->set($this->prefix . $relation_name, $this->loaded_attaches[$relation_name][$object->get($this->prefix . $this->base_object->getPrimaryKeyName())]);
         break;
         case lmbActiveRecord :: HAS_MANY:
         case lmbActiveRecord :: HAS_MANY_TO_MANY:
-          $collection = $this->base_object->createRelationCollection($relation_name);
-          $collection->setDataset(new lmbCollection($this->loaded_attaches[$relation_name][$object->get($this->prefix . $this->base_object->getPrimaryKeyName())]));
-          $fields->set($this->prefix . $relation_name, $collection);
+          if(isset($this->loaded_attaches[$relation_name][$object->get($this->prefix . $this->base_object->getPrimaryKeyName())]))
+          {
+            $collection = $this->base_object->createRelationCollection($relation_name);
+            $collection->setDataset(new lmbCollection($this->loaded_attaches[$relation_name][$object->get($this->prefix . $this->base_object->getPrimaryKeyName())]));
+            $fields->set($this->prefix . $relation_name, $collection);
+          }
         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-20 19:06:51 UTC (rev 6709)
+++ 3.x/trunk/limb/active_record/tests/cases/lmbARQueryTest.class.php	2008-01-21 07:52:52 UTC (rev 6710)
@@ -9,7 +9,8 @@
  
 class lmbARQueryTest extends lmbARBaseTestCase
 {
-  protected $tables_to_cleanup = array('test_one_table_object', 
+  protected $tables_to_cleanup = array('test_one_table_object',
+                                       'program_for_test',
                                        'person_for_test', 
                                        'social_security_for_test',
                                        'lecture_for_test',
@@ -627,6 +628,53 @@
     $this->assertEqual($arr[0]->getProgram()->getTitle(), $program->getTitle());
     $this->assertNull($arr[1]->getProgram());
   }  
+
+  function testFetch_AttachWithNothingToAttach()
+  {
+    $program1 = $this->creator->createProgram();
+    $program2 = $this->creator->createProgram();
+    
+    $query = new lmbARQuery('ProgramForTest', $this->conn);
+    $arr = $query->attach('courses')->fetch()->getArray();
+    
+    $this->assertEqual($arr[0]->getTitle(), $program1->getTitle());
+    $this->assertEqual($arr[1]->getTitle(), $program2->getTitle());
+    $this->assertEqual($arr[0]->getCourses()->count(), 0);
+    $this->assertEqual($arr[1]->getCourses()->count(), 0);
+  }  
+
+  function testFetch_JoinWithWrongRelationType()
+  {
+    $program1 = $this->creator->createProgram();
+    $program2 = $this->creator->createProgram();
+    
+    $query = new lmbARQuery('ProgramForTest', $this->conn);
+    $query->join('courses');
+    try
+    {
+      $it = $query->fetch();
+      $this->assertTrue(false);
+    }
+    catch(lmbARException $e)
+    {
+      $this->assertTrue(true);
+    }
+  }  
+  
+  function testFetch_AttachManyBelongsToRelationWithNothingToAttach()
+  {
+    $course1 = $this->creator->createCourse();
+    $course2 = $this->creator->createCourse();
+    
+    $query = new lmbARQuery('CourseForTest', $this->conn);
+    $arr = $query->attach('program')->fetch()->getArray();
+
+    $this->assertEqual($arr[0]->getTitle(), $course1->getTitle());
+    $this->assertEqual($arr[1]->getTitle(), $course2->getTitle());
+
+    $this->assertNull($arr[0]->getProgram(), 0);
+    $this->assertNull($arr[1]->getProgram(), 0);
+  }
 }
 
 



More information about the limb-svn mailing list