- Delay counting until the count is requested, to avoid potentially expensive queries
authorChuck Hagenbuch <chuck@horde.org>
Wed, 2 Sep 2009 04:22:59 +0000 (00:22 -0400)
committerChuck Hagenbuch <chuck@horde.org>
Wed, 2 Sep 2009 04:22:59 +0000 (00:22 -0400)
- Throw an exception if a mapper isn't passed with one of the non-object $query variants

framework/Rdo/lib/Horde/Rdo/List.php

index 013cf76..5d3a3be 100644 (file)
 class Horde_Rdo_List implements Iterator, Countable
 {
     /**
+     * Rdo Query
+     * @var mixed
+     */
+    protected $_query;
+
+    /**
      * Rdo Mapper
      * @var Horde_Rdo_Mapper
      */
@@ -95,10 +101,12 @@ class Horde_Rdo_List implements Iterator, Countable
             list($this->_sql, $this->_bindParams) = $query;
         }
 
-        // Keep a handle on the Mapper object for running the query.
-        $this->_mapper = $mapper;
+        if (!$mapper) {
+            throw new Horde_Rdo_Exception('Mapper must be provided either explicitly or in a Query object');
+        }
 
-        $this->_count = $this->_mapper->count($query);
+        $this->_query = $query;
+        $this->_mapper = $mapper;
     }
 
     /**
@@ -198,8 +206,16 @@ class Horde_Rdo_List implements Iterator, Countable
         return !$this->_eof;
     }
 
+    /**
+     * Implementation of count() for Countable
+     *
+     * @return integer Number of elements in the list
+     */
     public function count()
     {
+        if (is_null($this->_count)) {
+            $this->_count = $this->_mapper->count($this->_query);
+        }
         return $this->_count;
     }