diff options
author | Tim Almdal <tnalmdal@shaw.ca> | 2008-11-20 05:06:24 +0000 |
---|---|---|
committer | Tim Almdal <tnalmdal@shaw.ca> | 2008-11-20 05:06:24 +0000 |
commit | 604e58346bdbb99fa62f53aa21d37f0d239b93fc (patch) | |
tree | 32c62505612b5e54e343f53d9612ec6b38a49a57 /core/libraries | |
parent | 29cc48ebcf2f8c267a41815cb1cf0ea11f7326e2 (diff) |
Add unittest and caching to ORM_MTPP::descendants_count
Diffstat (limited to 'core/libraries')
-rw-r--r-- | core/libraries/ORM_MPTT.php | 18 |
1 files changed, 9 insertions, 9 deletions
diff --git a/core/libraries/ORM_MPTT.php b/core/libraries/ORM_MPTT.php index 5cdf9c02..c9c29805 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_count = array(); function __construct($id=null) { parent::__construct($id); @@ -165,19 +166,18 @@ class ORM_MPTT_Core extends ORM { * @return integer child count */ function descendants_count($type=null) { - // @todo create a unit test - // @todo set up caching in an array;; using type=all allows us to cache as decendents[$type] - $this->where("left >=", $this->left) - ->where("right <=", $this->right); - if ($type) { - $this->where("type", $type); + if (!isset($this->descendants_count[$type])) { + $this->where("left >", $this->left) + ->where("right <=", $this->right); + if ($type) { + $this->where("type", $type); + } + $this->descendants_count[$type] = $this->count_all(); } - // @todo does it make sense to order it before counting? - return $this->count_all(); + return $this->descendants_count[$type]; } - /** * @see ORM::reload */ |