summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/libraries/ORM_MPTT.php24
-rw-r--r--core/tests/ORM_MPTT_Test.php36
2 files changed, 48 insertions, 12 deletions
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];
}
/**
diff --git a/core/tests/ORM_MPTT_Test.php b/core/tests/ORM_MPTT_Test.php
index a053b61e..2b334243 100644
--- a/core/tests/ORM_MPTT_Test.php
+++ b/core/tests/ORM_MPTT_Test.php
@@ -95,6 +95,42 @@ class ORM_MPTT_Test extends Unit_Test_Case {
$this->assert_equal(2, $outer->children_count());
}
+ public function descendant_test() {
+ $parent = album::create(1, "parent album", "parent album title");
+ photo::create($parent->id, DOCROOT . "themes/default/images/thumbnail.jpg", "photo1",
+ "photo1", "parent album photo");
+
+ $album1 = album::create($parent->id, "album1", "album1 title");
+ photo::create($album1->id, DOCROOT . "themes/default/images/thumbnail.jpg", "photo1",
+ "photo1", "album1 photo");
+
+ $album2 = album::create($parent->id, "album2", "album2 title");
+ photo::create($album2->id, DOCROOT . "themes/default/images/thumbnail.jpg", "photo2",
+ "photo2", "album2 photo");
+ $parent->reload();
+
+ $this->assert_equal(5, $parent->descendants()->count());
+ $this->assert_equal(3, $parent->descendants(null, 0, "photo")->count());
+ $this->assert_equal(2, $parent->descendants(null, 0, "album")->count());
+ }
+
+ public function descendant_limit_test() {
+ $parent = album::create(1, "parent album", "parent album title");
+ photo::create($parent->id, DOCROOT . "themes/default/images/thumbnail.jpg", "photo1",
+ "photo1", "parent album photo");
+
+ $album1 = album::create($parent->id, "album1", "album1 title");
+ photo::create($album1->id, DOCROOT . "themes/default/images/thumbnail.jpg", "photo1",
+ "photo1", "album1 photo");
+
+ $album2 = album::create($parent->id, "album2", "album2 title");
+ photo::create($album2->id, DOCROOT . "themes/default/images/thumbnail.jpg", "photo2",
+ "photo2", "album2 photo");
+ $parent->reload();
+
+ $this->assert_equal(2, $parent->descendants(2)->count());
+ }
+
public function descendant_count_test() {
$parent = album::create(1, "parent album", "parent album title");
photo::create($parent->id, DOCROOT . "themes/default/images/thumbnail.jpg", "photo1",