mirror of
https://github.com/Pathduck/gallery3.git
synced 2026-04-29 17:49:15 -04:00
Add children_count() to ORM_MPTT
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user