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

svn at limb-project.com svn at limb-project.com
Mon Jun 9 16:28:30 MSD 2008


Author: serega
Date: 2008-06-09 16:28:30 +0400 (Mon, 09 Jun 2008)
New Revision: 7059
URL: http://fisheye.limb-project.com/changelog/limb/?cs=7059

Modified:
   3.x/trunk/limb/active_record/src/lmbActiveRecord.class.php
   3.x/trunk/limb/active_record/tests/cases/lmbAROneToManyRelationsTest.class.php
   3.x/trunk/limb/active_record/tests/cases/lmbAROneToOneRelationsTest.class.php
Log:
-- $has_one and $many_belongs_to relations now supports 'throw_exception_on_not_found' relation definition flag. If this flag is used and object can't be loaded (it was removed from database and relation field were not update) then active record will not raise an exception.

Modified: 3.x/trunk/limb/active_record/src/lmbActiveRecord.class.php
===================================================================
--- 3.x/trunk/limb/active_record/src/lmbActiveRecord.class.php	2008-06-06 11:00:29 UTC (rev 7058)
+++ 3.x/trunk/limb/active_record/src/lmbActiveRecord.class.php	2008-06-09 12:28:30 UTC (rev 7059)
@@ -862,9 +862,15 @@
     $value = $this->_getRaw($this->_many_belongs_to[$property]['field']);
     if(!$value && $this->_canManyBelongsToObjectBeNull($property))
       return null;
+    
+    if(isset($this->_many_belongs_to[$property]['throw_exception_on_not_found']))
+      $throw_exception = $this->_many_belongs_to[$property]['throw_exception_on_not_found'];
+    else
+      $throw_exception = true;
 
     return self :: findById($this->_many_belongs_to[$property]['class'],
                             $this->get($this->_many_belongs_to[$property]['field']),
+                            $throw_exception, 
                             $this->_db_conn);
   }
 
@@ -873,9 +879,15 @@
     $value = $this->_getRaw($this->_has_one[$property]['field']);
     if(!$value && $this->_canHasOneObjectBeNull($property))
       return null;
+    
+    if(isset($this->_has_one[$property]['throw_exception_on_not_found']))
+      $throw_exception = $this->_has_one[$property]['throw_exception_on_not_found'];
+    else
+      $throw_exception = true;
 
     return self :: findById($this->_has_one[$property]['class'],
                             $this->get($this->_has_one[$property]['field']),
+                            $throw_exception,
                             $this->_db_conn);
   }
 

Modified: 3.x/trunk/limb/active_record/tests/cases/lmbAROneToManyRelationsTest.class.php
===================================================================
--- 3.x/trunk/limb/active_record/tests/cases/lmbAROneToManyRelationsTest.class.php	2008-06-06 11:00:29 UTC (rev 7058)
+++ 3.x/trunk/limb/active_record/tests/cases/lmbAROneToManyRelationsTest.class.php	2008-06-09 12:28:30 UTC (rev 7059)
@@ -40,12 +40,11 @@
   protected $_db_table_name = 'lecture_for_test';
   protected $_many_belongs_to = array('course' => array('field' => 'course_id',
                                                         'class' => 'CourseWithNullableLectures',
-                                                        'can_be_null' => true),
+                                                        'can_be_null' => true,
+                                                        'throw_exception_on_not_found' => false),
                                       );
 }
 
-
-
 Mock :: generate('LectureForTest', 'MockLectureForTest');
 
 class lmbAROneToManyRelationsTest extends lmbARBaseTestCase
@@ -204,6 +203,44 @@
     $this->assertEqual($lecture3->getAltCourse()->getTitle(), $course->getTitle());
   }
 
+  function testLoadingNonExistingParentThrowsExceptionByDefault()
+  {
+    $course = $this->_initCourse();
+
+    $lecture = new LectureForTest();
+    $lecture->setTitle('Physics');
+    $lecture->setCourse($course);
+    $lecture->save();
+    
+    $this->db->delete('course_for_test', 'id = '. $course->getId());
+
+    $lecture2 = lmbActiveRecord :: findById('LectureForTest', $lecture->getId());
+    try
+    {
+      $lecture2->getCourse();
+      $this->assertTrue(false);
+    }
+    catch(lmbARNotFoundException $e)
+    {
+      $this->assertTrue(true);
+    }
+  }
+  
+  function testLoadingNonExistingParent_NOT_ThrowsException_IfSpecialFlagUsedForRelationDefinition()
+  {
+    $course = $this->_initCourse();
+
+    $lecture = new LectureIndependentFromCourse();
+    $lecture->setTitle('Physics');
+    $lecture->setCourse($course);
+    $lecture->save();
+    
+    $this->db->delete('course_for_test', 'id = '. $course->getId());
+
+    $lecture2 = lmbActiveRecord :: findById('LectureIndependentFromCourse', $lecture->getId());
+    $this->assertNull($lecture2->getCourse());
+  }
+
   function testSettingNullParentObject()
   {
     $course = $this->_initCourse();

Modified: 3.x/trunk/limb/active_record/tests/cases/lmbAROneToOneRelationsTest.class.php
===================================================================
--- 3.x/trunk/limb/active_record/tests/cases/lmbAROneToOneRelationsTest.class.php	2008-06-06 11:00:29 UTC (rev 7058)
+++ 3.x/trunk/limb/active_record/tests/cases/lmbAROneToOneRelationsTest.class.php	2008-06-09 12:28:30 UTC (rev 7059)
@@ -16,6 +16,15 @@
                                                          'cascade_delete' => false));
 }
 
+class PersonForTestWithNotRequiredSocialSecurity extends lmbActiveRecord
+{
+  protected $_db_table_name = 'person_for_test';
+  protected $_has_one = array('social_security' => array('field' => 'ss_id',
+                                                         'class' => 'SocialSecurityForTest',
+                                                         'can_be_null' => true,
+                                                         'throw_exception_on_not_found' => false));
+}
+
 class PersonForTestWithRequiredSocialSecurity extends lmbActiveRecord
 {
   protected $_db_table_name = 'person_for_test';
@@ -206,7 +215,43 @@
     $this->assertEqual($number2->getId(), $number->getId());
     $this->assertEqual($number2->getCode(), $number->getCode());
   }
+  
+  function testLoadNonExistingChildObject_ThrowsExceptionByDefault()
+  {
+    $person = $this->creator->initPerson();
+    $number = $this->creator->initSocialSecurity();
+    $person->setSocialSecurity($number);
+    $person->save();
+    
+    $this->db->delete('social_security_for_test', 'id = '. $number->getId());
 
+    $person2 = lmbActiveRecord :: findById('PersonForTest', $person->getId());
+
+    try
+    {
+      $person2->getSocialSecurity();
+      $this->assertTrue(false);
+    }
+    catch(lmbARNotFoundException $e)
+    {
+      $this->assertTrue(true);
+    }
+  }
+
+  function testLoadNonExistingChildObject_NOT_ThrowsException_IfSpecialFlagUsedInRelationDefinition()
+  {
+    $person = $this->creator->initPerson();
+    $number = $this->creator->initSocialSecurity();
+    $person->setSocialSecurity($number);
+    $person->save();
+    
+    $this->db->delete('social_security_for_test', 'id = '. $number->getId());
+
+    $person2 = lmbActiveRecord :: findById('PersonForTestWithNotRequiredSocialSecurity', $person->getId());
+
+    $this->assertNull($person2->getSocialSecurity());
+  }
+
   function testGenericGetLoadsParentObject()
   {
     $person = $this->creator->initPerson();



More information about the limb-svn mailing list