From 6fd261c8b02c90d6a24d12b10e5bd20a0adb4734 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Thu, 20 Nov 2008 06:05:22 +0000 Subject: Add unittest and caching for ORM_MTPP::descendants --- core/libraries/ORM_MPTT.php | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'core/libraries') diff --git a/core/libraries/ORM_MPTT.php b/core/libraries/ORM_MPTT.php index c9c29805..88e79194 100644 --- a/core/libraries/ORM_MPTT.php +++ b/core/libraries/ORM_MPTT.php @@ -37,6 +37,7 @@ class ORM_MPTT_Core extends ORM { private $parents = null; private $children = null; private $children_count = null; + private $descendants = array(); private $descendants_count = array(); function __construct($id=null) { @@ -143,20 +144,19 @@ class ORM_MPTT_Core extends ORM { * @return object ORM_Iterator */ function descendants($limit=NULL, $offset=0, $type=null) { - // @todo create a unit test - // @todo set up caching in an array; using type=all allows us to cache as decendents[$type] - // @todo needs to take into account the offset and limit as well - $this->where("left >=", $this->left) - ->where("right <=", $this->right); - if ($type) { - $this->where("type", $type); - } + if (!isset($this->descendants[$type][$offset])) { + $this->where("left >", $this->left) + ->where("right <=", $this->right); + if ($type) { + $this->where("type", $type); + } - // @todo: make the order column data driven - $this->orderby("id", "ASC"); + // @todo: make the order column data driven + $this->orderby("id", "ASC"); - $descendants = $this->find_all($limit, $offset); - return $descendants; + $this->descendants[$type][$offset] = $this->find_all($limit, $offset); + } + return $this->descendants[$type][$offset]; } /** -- cgit v1.2.3