{@link TreeNode} collection object.
Any objects used with this class must support Object::getID(). Extension of this class allows collections of objects to be collated, used and manipulated in a convenient and consistent manner. The child class is responsible for loading the objects and will often provide function(s) for loading and deleting objects. An example function for loading all records as objects into the collection:
public function loadAll() {
parent::clear(); // clears any objects in collection
$rs = DataManager::loadObjects(); // load id's from the database to a var
foreach ($rs as $row) {
parent::add(ObjectClass::loadByID($row[0])); // load objects into this class. This may vary with the actual class method being called.
}
}
package | dstruct_tree |
---|---|
author | David |
add(object $obj) : boolean
If the object exists, a DStructGeneralException will be thrown
inherited_from | \ObjCollection::add() |
---|
object
\DStructGeneralException |
---|
boolean
count() : integer
inherited_from | \ObjCollection::count() |
---|
integer
exists(mixed $obj) : boolean
inherited_from | \ObjCollection::exists() |
---|
mixed
Object, or it's ID
boolean
getArray() : array
inherited_from | \ObjCollection::getArray() |
---|
array
getByID(integer $id) : object | false
inherited_from | \ObjCollection::getByID() |
---|
integer
object
false
getFirst() : object | false
inherited_from | \ObjCollection::getFirst() |
---|
object
false
loadAllAcceptingMembers(\Tree | integer $tree)
loadByParent(\TreeNode | integer $parent, \Tree | integer $tree)
prev() : mixed
inherited_from | \ObjCollection::prev() |
---|
mixed
object or false.remove(mixed $obj) : boolean
If the object is not in the collection, a DStructGeneralException will be thrown
inherited_from | \ObjCollection::remove() |
---|
mixed
Object, or its ID
\DStructGeneralException |
---|
boolean
sortObjects(string $element, integer $sort_direction, boolean $caseinsensitive, array $params, boolean $astime)
WARNING: This method may be slow! It is advisable to use another method if one is available,
for instance, using ORDER BY in an SQL statement to create and add the objects in order.
The $element parameter can specify either an attribute or a method of
the objects in the collection. Methods should be named in full e.g. "method()".
Although this uses the PHP function usort(), the array keys are preserved.
The constants SORT_OBJECTS_ASC and SORT_OBJECTS_DESC are provided
to set the $sort_direction parameter.
If you set $astime to true, the method will try to use PHP's strtotime() function to
attempt to convert strings into time, allowing you to sort dates in the expected order. You must
ensure that all data will convert to strtotime, or that data will be evalutated as false!
Example:
class Employee {
public $name;
public function __construct($name) {$this->name = $name;}
public function getName() {return $this->name;}
}
$employees = new Employees; // create collection object
$employees->add(new Employee('neil'));
$employees->add(new Employee('David'));
$employees->add(new Employee('Shane'));
// order by name attribute
$employees->sortObjects('name');
// new order: David, neil, Shane
// order by method return, and case sensitive, and passing a single parameter of true to the method
$employees->sortObjects('getName()', Employees::SORT_ORDERS_ASC, false, array(true));
// new order: David, Shane, neil
inherited_from | \ObjCollection::sortObjects() |
---|
string
Element of the objects to sort by
integer
1 (ascending) or -1 (descending). See above.
boolean
Case sensitivity of sort.
array
Parameters to be passed on if calling a method
boolean
Try to use strtotime() to sort by date
clear()
inherited_from | \ObjCollection::clear() |
---|
csort_cmp_attribute(object $a, object $b) : integer
inherited_from | \ObjCollection::csort_cmp_attribute() |
---|
object
object
integer
0 or 1csort_cmp_method(object $a, object $b) : integer
inherited_from | \ObjCollection::csort_cmp_method() |
---|
object
object
integer
0 or 1$objs : array
inherited_from | \ObjCollection::$$objs |
---|
SORT_OBJECTS_ASC : integer
inherited_from | \ObjCollection::SORT_OBJECTS_ASC |
---|
SORT_OBJECTS_DESC : integer
inherited_from | \ObjCollection::SORT_OBJECTS_DESC |
---|