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

svn at limb-project.com svn at limb-project.com
Tue Jun 5 10:30:30 MSD 2007


Author: pachanga
Date: 2007-06-05 10:30:30 +0400 (Tue, 05 Jun 2007)
New Revision: 5936
URL: http://fisheye.limb-project.com/changelog/limb/?cs=5936

Modified:
   3.x/trunk/limb/active_record/src/lmbActiveRecord.class.php
   3.x/trunk/limb/active_record/tests/cases/lmbActiveRecordTest.class.php
Log:
-- lmbActiveRecord :: findById(..) third argument added, $not_found_exception = true which makes this method throw lmbARNotFoundException exception if no such object was found. Setting it to false overrides this behavior and this method simply returns null.
-- lmbActiveRecord :: find((int)$id) now doesn't throw lmbARNotFoundException exception, it simply returns null

Modified: 3.x/trunk/limb/active_record/src/lmbActiveRecord.class.php
===================================================================
--- 3.x/trunk/limb/active_record/src/lmbActiveRecord.class.php	2007-06-05 05:54:29 UTC (rev 5935)
+++ 3.x/trunk/limb/active_record/src/lmbActiveRecord.class.php	2007-06-05 06:30:30 UTC (rev 5936)
@@ -1,13 +1,13 @@
 <?php
-/*
- * Limb PHP Framework
- *
- * @link http://limb-project.com
- *
- * @copyright  Copyright &copy; 2004-2007 BIT
- * @license    LGPL http://www.gnu.org/copyleft/lesser.html
- * @version    $Id$
- * @package    $package$
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ *
+ * @copyright  Copyright &copy; 2004-2007 BIT
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ * @version    $Id$
+ * @package    $package$
  */
 lmb_require('limb/core/src/lmbObject.class.php');
 lmb_require('limb/core/src/lmbDelegate.class.php');
@@ -1056,13 +1056,13 @@
    *  @param integer object id
    *  @return object|null
    */
-  static function findById($class_name, $id)
+  static function findById($class_name, $id, $not_found_exception = true)
   {
     if(!class_exists($class_name, true))
       throw new lmbARException("Could not find class '$class_name'");
 
     $obj = new $class_name();
-    return $obj->_findById($id);
+    return $obj->_findById($id, $not_found_exception);
   }
   /**
    *  Userland filter for findById() static method
@@ -1070,12 +1070,14 @@
    *  @param integer object id
    *  @return object
    */
-  protected function _findById($id)
+  protected function _findById($id, $not_found_exception)
   {
     if($object = lmbActiveRecord :: find(get_class($this), array('first', 'criteria' => 'id=' . (int)$id)))
       return $object;
+    elseif($not_found_exception)
+      throw new lmbARNotFoundException(get_class($this), $id);
     else
-      throw new lmbARNotFoundException(get_class($this), $id);
+      return null;
   }
   /**
    *  Finds a collection of objects in database using array of object ids, this method is actually a wrapper around find()
@@ -1195,7 +1197,7 @@
     if(self :: _isCriteria($magic_params))
       $params = array('criteria' => $magic_params);
     elseif(is_int($magic_params))
-      return self :: findById($class_name, $magic_params);
+      return self :: findById($class_name, $magic_params, false);
     elseif(!is_array($magic_params))
       throw new lmbARException("Invalid magic params", array($magic_params));
     else

Modified: 3.x/trunk/limb/active_record/tests/cases/lmbActiveRecordTest.class.php
===================================================================
--- 3.x/trunk/limb/active_record/tests/cases/lmbActiveRecordTest.class.php	2007-06-05 05:54:29 UTC (rev 5935)
+++ 3.x/trunk/limb/active_record/tests/cases/lmbActiveRecordTest.class.php	2007-06-05 06:30:30 UTC (rev 5936)
@@ -1,13 +1,13 @@
 <?php
-/*
- * Limb PHP Framework
- *
- * @link http://limb-project.com
- *
- * @copyright  Copyright &copy; 2004-2007 BIT
- * @license    LGPL http://www.gnu.org/copyleft/lesser.html
- * @version    $Id$
- * @package    $package$
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ *
+ * @copyright  Copyright &copy; 2004-2007 BIT
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ * @version    $Id$
+ * @package    $package$
  */
 lmb_require('limb/active_record/src/lmbActiveRecord.class.php');
 lmb_require('limb/dbal/src/lmbSimpleDb.class.php');
@@ -275,6 +275,11 @@
     catch(lmbARException $e){}
   }
 
+  function testFindByIdReturnsNullIfNotFound()
+  {
+    $this->assertNull(lmbActiveRecord :: findById($this->class_name, -1000, false));
+  }
+
   function testLoadById()
   {
     $object1 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
@@ -475,7 +480,7 @@
     $this->assertFalse($rs->valid());
   }
 
-  function testFindWithIntCallsFindById()
+  function testFindWithIntegerCallsFindById()
   {
     $object1 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
     $object2 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
@@ -484,6 +489,11 @@
     $this->assertEqual($object2->getId(), $object->getId());
   }
 
+  function testFindWithIntegerDoesNotThrowException()
+  {
+    $this->assertNull(lmbActiveRecord :: find($this->class_name, -10000));
+  }
+
   function testFindByThrowsExceptionIfMagicParamsIsNull()
   {
     try



More information about the limb-svn mailing list