summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorBharat Mediratta <bharat@menalto.com>2008-11-07 06:46:38 +0000
committerBharat Mediratta <bharat@menalto.com>2008-11-07 06:46:38 +0000
commit936decb437cbb19799dcae255af1d4425a850ae3 (patch)
tree6edc9bd258d255f89b0d98d389feae449fcc147e /core
parent6a1ef44c8842d007dd59574cc76ba767519d124f (diff)
Add children_count() to ORM_MPTT
Diffstat (limited to 'core')
-rw-r--r--core/libraries/ORM_MPTT.php30
-rw-r--r--core/tests/ORM_MPTT_Test.php25
2 files changed, 53 insertions, 2 deletions
diff --git a/core/libraries/ORM_MPTT.php b/core/libraries/ORM_MPTT.php
index 83190d4a..f79ea910 100644
--- a/core/libraries/ORM_MPTT.php
+++ b/core/libraries/ORM_MPTT.php
@@ -36,6 +36,7 @@ class ORM_MPTT_Core extends ORM {
private $parent = null;
private $parents = null;
private $children = null;
+ private $children_count = null;
function __construct($id=null) {
parent::__construct($id);
@@ -102,22 +103,47 @@ class ORM_MPTT_Core extends ORM {
/**
* Return all of the children of this node, ordered by id.
*
+ * @chainable
+ * @param integer SQL limit
+ * @param integer SQL offset
* @return array ORM
*/
- function children() {
+ function children($limit=NULL, $offset=0) {
if (!isset($this->children)) {
$this->children =
$this->where("parent_id", $this->id)
->orderby("id", "ASC")
- ->find_all();
+ ->find_all($limit, $offset);
}
return $this->children;
}
+ /**
+ * Return all of the children of this node, ordered by id.
+ *
+ * @chainable
+ * @param integer SQL limit
+ * @param integer SQL offset
+ * @return array ORM
+ */
+ function children_count() {
+ if (!isset($this->children_count)) {
+ $this->children_count =
+ $this->where("parent_id", $this->id)
+ ->orderby("id", "ASC")
+ ->count_all();
+ }
+ return $this->children_count;
+ }
+
+ /**
+ * @see ORM::reload
+ */
function reload() {
$this->parent = null;
$this->parents = null;
$this->children = null;
+ $this->children_count = null;
return parent::reload();
}
diff --git a/core/tests/ORM_MPTT_Test.php b/core/tests/ORM_MPTT_Test.php
index a543c1cd..d66006f5 100644
--- a/core/tests/ORM_MPTT_Test.php
+++ b/core/tests/ORM_MPTT_Test.php
@@ -69,4 +69,29 @@ class ORM_MPTT_Test extends Unit_Test_Case {
}
$this->assert_equal(array($inner1->id, $inner2->id), $child_ids);
}
+
+ public function children_limit_test() {
+ $outer = ORM::factory("item");
+ $outer->add_to_parent(1);
+
+ $inner1 = ORM::factory("item");
+ $inner1->add_to_parent($outer->id);
+
+ $inner2 = ORM::factory("item");
+ $inner2->add_to_parent($outer->id);
+
+ $this->assert_equal(array($inner2->id => null), $outer->children(1, 1)->select_list('id'));
+ }
+
+ public function children_count_test() {
+ $outer = ORM::factory("item");
+ $outer->add_to_parent(1);
+
+ $inner1 = ORM::factory("item");
+ $inner1->add_to_parent($outer->id);
+
+ $inner2 = ORM::factory("item");
+ $inner2->add_to_parent($outer->id);
+ $this->assert_equal(2, $outer->children_count());
+ }
}