Class: NestedSet
An implementation of the Nested Set algorithm, which allows hierarchies and
trees to be stored efficiently in a relational database table structure.
Usage Example
<?php
$ns = new NestedSet ('table_name');
// create a new collection (you only need to call this once)
$res = $ns->create (array (
'name' => 'char(32) not null',
'position' => 'char(32) not null',
));
if (! $res) {
die ($ns->error);
}
// add a document root (you can have as many as you want)
$res = $ns->addRoot (array (
'id' => 'index',
'name' => 'Employee Directory',
'position' => '',
));
if (! $res) {
die ($ns->error);
}
// omitting error checking from here down
// add some items
$ns->add ('index', array (
'id' => 'joe_smith',
'name' => 'Joe Smith',
'position' => 'Boss',
));
$ns->add ('joe_smith', array (
'id' => 'sam_jones',
'name' => 'Samantha Jones',
'position' => 'Assistant',
));
// view the path to sam_jones
info ($ns->path ('sam_jones'));
// delete joe_smith
$ns->delete ('joe_smith');
// promote sam_jones
$ns->edit ('sam_jones', array (
'position' => 'Boss',
));
?>
Return to Top
Properties
$connection
The current connection resource.
$path
Path to the Berkeley Database.
$mode
Mode to use when opening the database (corresponds to
the modes available to the dba_open () function.
$handler
The database implementation used (db2, db3, gdbm, etc.).
$persistent
A 1 or 0 (true or false, and true by default), specifying whether
to establish a persistent connection or not.
$transactions = 0
Boolean value denoting whether to enable transactions in the
current database.
$driver
Contains the name of the database driver being used.
$host
Contains the name of the database host.
$name
Contains the name of the database being used.
$user
Contains the username used to connect to the current database.
$pass
Contains the password used to connect to the current database.
$dbd
Contains the loaded database driver.
$error
Error message, in case of error.
$sql = ''
Contains the SQL query to be executed.
$rows
Contains the number of rows returned by the previous fetch() call.
$lastid
Contains the lastid() of the last insert query sent to the execute()
method.
$tables = false
Contains a list of tables in the database. Set by getTables().
$pearEmu = false
$sequenceFormat = '%s_seq'
$fetchMode = DB_FETCHMODE_OBJECT
$result = ''
Contains the result identifier for the current execution.
$field = ''
Currently unused.
$_fetchModeFunctions = array (
DB_FETCHMODE_ASSOC => 'mysql_fetch_array',
DB_FETCHMODE_OBJECT => 'mysql_fetch_object',
)
$typemap = array (
'bpchar' => 'text',
'char' => 'text',
'varchar' => 'text',
'text' => 'textarea',
'date' => 'date',
'time' => 'time',
'timestamp' => 'datetime',
'.*' => 'text',
// arrays
// booleans
// blobs
)
Contains a key/value list of database types (regular
expressions are used here to save repeating ourselves) and their
corresponding MailForm widget types.
$column
Column name of the facet.
$title
Dispaly name of the facet.
$extra
Extra info about the facet.
$tableObj
DatabaseTable object.
$items = array ()
List of options in the facet.
$type = 'normal'
Type of the facet. Possible values are 'normal', 'self_ref', 'date', and 'time'.
This value is auto-determined by compile(), but can be customized as well.
$table
Table name.
$fields = array ()
Field list.
$pkey = ''
Primary key field.
$fkey = ''
Optional name of a foreign key field, for use in simplified find() calls.
$listFields = '*'
The list of fields to return on find(). Default is '*' to return all fields.
$isAuto = true
Whether the pkey is auto-incrementing or not.
$orderBy = ''
Contains the "order by" clause value (ie. "name asc") for calls to find().
$groupBy = ''
Contains the "group by" clause value (ie. "name asc") for calls to find().
$limit = false
Contains the "limit" clause value for calls to find().
$offset = false
Contains the "offset" clause value for calls to find().
$invalid = array ()
A list of invalid fields and their corresponding error messages,
from the previous validate() call.
$usePermissions = false
Determines whether to automatically add access control to find(), count(),
and get() calls.
$multilingual = false
Enables automatic translation of items pulled from the database through
Sitellite's new multilingual capabilities.
$_cascade = array ()
This is the list of external types to cascade deletions across. In the
case of external objects, the keys are the object names and the values
are the referencing field in the external object's table. In the case
of join tables for many-to-many relationships, the keys are simply
numeric and the value is an array containing the join table name and the
name of the field referring to the current object's primary key field.
Ordinarily, these values are set automatically via the load() method,
and the relationships are defined in an INI file.
$server
The name of the server to connect to.
$port
The port to use to connect to the server.
$rdn
The rdn (username, essentially) to use to bind to
the connection.
$password
The password to use to bind to the connection.
$secure
Whether to use TLS for LDAP connections.
$resource
The current search resource.
$firstCalled
Says whether or not the first search result entry has
been returned.
$errno
The errno of the previous error.
$connections = array ()
Connection list.
$current
Active tree root ID.
$key
Primary key field name.
$root
Root field name.
$left
Left field name.
$right
Right field name.
$order
Sorting order field name.
$level
Level field name.
$indexes = array ()
Index list.
Return to Top
Methods
NestedSet ($table = 'sitellite_page', $key = 'id', $root = 'ns_root', $left = 'ns_left', $right = 'ns_right', $order = 'ns_order', $level = 'ns_level')
Constructor method. If your table was created by this class, then you
probably only need to specify the $table and $key.
create ($fields = array ())
- Access: public
- Return: boolean
Creates a new table with the specified fields (not including the nested
set-specific fields (ns_root, ns_left, etc.
root ($val = false)
- Access: public
- Return: string
Gets or sets the current root node. Gets the root if $val is
false, and sets it if it's not, returning the previous root
in this case.
addRoot ($values = array (), $setCurrent = true)
- Access: public
- Return: boolean
Adds a new root node to the tree.
add ($pid, $values = array ())
- Access: public
- Return: boolean
Adds a new node (aka item) to the tree.
edit ($id, $newValues = array ())
- Access: public
- Return: boolean
Modifies the specified item.
delete ($id, $recursive = false)
- Access: public
- Return: boolean
Removes the specified node (item). If $recursive is true
then it also removes all nodes below the specified node.
move ($id, $newParentId)
- Access: public
- Return: boolean
Moves the specified node (item) to under the specified parent ID.
rename ($id, $newId)
- Access: public
- Return: boolean
Renames the specified node (item).
get ($id, $fields = array ())
- Access: public
- Return: object
Retrieves the specified node. $fields allows you to optionally specify
which fields you want to retrieve. If left empty, it returns all of
them.
getRoots ($fields = array ())
- Access: public
- Return: array of objects
Retrieves all of the root nodes. $fields allows you to optionally specify
which fields you want to retrieve. If left empty, it returns all of
them.
find ($queries = array (), $fields = array (), $limit = 0, $offset = 0, $orderBy = false, $sort = 'ASC', $count = false)
- Access: public
- Return: array of objects
Performs a search on the tree. $fields allows you to optionally specify
which fields you want to retrieve. If left empty, it returns only the
key field.
siblings ($id)
- Access: public
- Return: array
Returns an array of the IDs of the sibling nodes of the specified node.
children ($id, $recursive = false)
- Access: public
- Return: array
Returns an array of the IDs of the child nodes of the specified node.
If $recursive is set to true, then the sorting is done by the ns_left
field instead of ns_order, so that the nodes are returned in the order
of the parents to which they belong.
path ($id, $fields = array ())
- Access: public
- Return: array of objects
Returns an array of the parent nodes of the specified node. This is
also known as a breadcrumb trail.
parent ($id, $fields = array ())
- Access: public
- Return: object
Returns the parent nodes of the specified node.
isParent ($pid, $id)
- Access: public
- Return: boolean
Checks whether $pid is a parent of $id.
_fields ($fields, $else = '*')
_where ($queries)
_set ($set)
_addRightNode ($sid, $values)
Return to Top