Change the children and descendants APIs to be more consistent and to

remove Gallery3 concepts from ORM_MPTT.

The following API methods:
  ORM_MPTT::children
  ORM_MPTT::children_count
  ORM_MPTT::descendants
  ORM_MPTT::descendants_count

All now take a $where clause that allow you to pass through additional
field parameters.

old API:
  $album->children(10, 0, "photos")
  $album->children_count("photos")

new API:
  $album->children(10, 0, array("type" => "photos"))
  $album->children_count(array("type" => "photos"))

This gives us a more flexible API and simplifies the code.  While I
was in there, I changed the way we deal with default orderby values so
that we just assign the default value in the function definition,
which allows us to get rid of all conditionals in the implementation
which results in simpler code.
This commit is contained in:
Bharat Mediratta
2009-08-05 10:38:53 -07:00
parent 9f396178ce
commit e8c57290a2
4 changed files with 51 additions and 56 deletions

View File

@@ -146,35 +146,30 @@ class ORM_MPTT_Core extends ORM {
* @chainable
* @param integer SQL limit
* @param integer SQL offset
* @param string type to return
* @param array additional where clauses
* @param array orderby
* @return array ORM
*/
function children($limit=null, $offset=0, $type=null, $orderby=null) {
$this->where("parent_id", $this->id);
if ($type) {
$this->where("type", $type);
}
if (empty($orderby)) {
$this->orderby("id", "ASC");
} else {
$this->orderby($orderby);
}
return $this->find_all($limit, $offset);
function children($limit=null, $offset=0, $where=array(), $orderby=array("id", "ASC")) {
return $this
->where("parent_id", $this->id)
->where($where)
->orderby($orderby)
->find_all($limit, $offset);
}
/**
* Return all of the children of this node, ordered by id.
*
* @chainable
* @param string type to return
* @param array additional where clauses
* @return array ORM
*/
function children_count($type=null) {
if ($type) {
$this->where("type", $type);
}
return $this->where("parent_id", $this->id)->count_all();
function children_count($where=array()) {
return $this
->where($where)
->where("parent_id", $this->id)
->count_all();
}
/**
@@ -182,39 +177,31 @@ class ORM_MPTT_Core extends ORM {
*
* @param integer SQL limit
* @param integer SQL offset
* @param string type to return
* @param array additional where clauses
* @param array orderby
* @return object ORM_Iterator
*/
function descendants($limit=null, $offset=0, $type=null, $orderby=null) {
$this->where("left_ptr >", $this->left_ptr)
->where("right_ptr <=", $this->right_ptr);
if ($type) {
$this->where("type", $type);
}
if (empty($orderby)) {
$this->orderby("id", "ASC");
} else {
$this->orderby($orderby);
}
return $this->find_all($limit, $offset);
function descendants($limit=null, $offset=0, $where=array(), $orderby=array("id", "ASC")) {
return $this
->where("left_ptr >", $this->left_ptr)
->where("right_ptr <=", $this->right_ptr)
->where($where)
->orderby($orderby)
->find_all($limit, $offset);
}
/**
* Return the count of all the children of the specified type.
*
* @param string type to count
* @param array additional where clauses
* @return integer child count
*/
function descendants_count($type=null) {
$this->where("left_ptr >", $this->left_ptr)
->where("right_ptr <=", $this->right_ptr);
if ($type) {
$this->where("type", $type);
}
return $this->count_all();
function descendants_count($where=array()) {
return $this
->where("left_ptr >", $this->left_ptr)
->where("right_ptr <=", $this->right_ptr)
->where($where)
->count_all();
}
/**