From c480d44f2a892e5fda216a1c560e6e11ec11c6e0 Mon Sep 17 00:00:00 2001 From: Chuck Hagenbuch Date: Wed, 2 Sep 2009 00:22:59 -0400 Subject: [PATCH] - Delay counting until the count is requested, to avoid potentially expensive queries - Throw an exception if a mapper isn't passed with one of the non-object $query variants --- framework/Rdo/lib/Horde/Rdo/List.php | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/framework/Rdo/lib/Horde/Rdo/List.php b/framework/Rdo/lib/Horde/Rdo/List.php index 013cf76e8..5d3a3bec8 100644 --- a/framework/Rdo/lib/Horde/Rdo/List.php +++ b/framework/Rdo/lib/Horde/Rdo/List.php @@ -15,6 +15,12 @@ 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; } -- 2.11.0