updated NestedSet to use Zend coding standards. still a few problems with return values using () that need to be looked at.
This commit is contained in:
parent
6786ce0ced
commit
2933cd2dd1
212
draft/Node.php
212
draft/Node.php
@ -31,119 +31,127 @@
|
|||||||
*/
|
*/
|
||||||
class Doctrine_Node implements IteratorAggregate
|
class Doctrine_Node implements IteratorAggregate
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @param object $record reference to associated Doctrine_Record instance
|
* @param object $record reference to associated Doctrine_Record instance
|
||||||
*/
|
*/
|
||||||
protected $record;
|
protected $record;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $options
|
* @param array $options
|
||||||
*/
|
*/
|
||||||
protected $options;
|
protected $options;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $iteratorType (Pre | Post | Level)
|
* @param string $iteratorType (Pre | Post | Level)
|
||||||
*/
|
*/
|
||||||
protected $iteratorType;
|
protected $iteratorType;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $iteratorOptions
|
* @param array $iteratorOptions
|
||||||
*/
|
*/
|
||||||
protected $iteratorOptions;
|
protected $iteratorOptions;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* contructor, creates node with reference to record and any options
|
* contructor, creates node with reference to record and any options
|
||||||
*
|
*
|
||||||
* @param object $record instance of Doctrine_Record
|
* @param object $record instance of Doctrine_Record
|
||||||
* @param array $options options
|
* @param array $options options
|
||||||
*/
|
*/
|
||||||
public function __construct(&$record, $options)
|
public function __construct(&$record, $options)
|
||||||
{
|
{
|
||||||
$this->record = $record;
|
$this->record = $record;
|
||||||
$this->options = $options;
|
$this->options = $options;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* factory method to return node instance based upon chosen implementation
|
* factory method to return node instance based upon chosen implementation
|
||||||
*
|
*
|
||||||
* @param object $record instance of Doctrine_Record
|
* @param object $record instance of Doctrine_Record
|
||||||
* @param string $impName implementation (NestedSet, AdjacencyList, MaterializedPath)
|
* @param string $impName implementation (NestedSet, AdjacencyList, MaterializedPath)
|
||||||
* @param array $options options
|
* @param array $options options
|
||||||
* @return object $options instance of Doctrine_Node
|
* @return object $options instance of Doctrine_Node
|
||||||
*/
|
*/
|
||||||
public static function factory(&$record, $implName, $options = array()) {
|
public static function factory(&$record, $implName, $options = array())
|
||||||
|
{
|
||||||
|
$class = 'Doctrine_Node_' . $implName;
|
||||||
|
|
||||||
$class = 'Doctrine_Node_'.$implName;
|
if (!class_exists($class)) {
|
||||||
|
throw new Doctrine_Node_Exception("The class $class must exist and extend Doctrine_Node");
|
||||||
|
}
|
||||||
|
|
||||||
if(!class_exists($class))
|
return new $class($record, $options);
|
||||||
throw new Doctrine_Node_Exception("The class $class must exist and extend Doctrine_Node");
|
}
|
||||||
|
|
||||||
return new $class($record, $options);
|
/**
|
||||||
}
|
* setter for record attribute
|
||||||
|
*
|
||||||
|
* @param object $record instance of Doctrine_Record
|
||||||
|
*/
|
||||||
|
public function setRecord(&$record)
|
||||||
|
{
|
||||||
|
$this->record = $record;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* setter for record attribute
|
* getter for record attribute
|
||||||
*
|
*
|
||||||
* @param object $record instance of Doctrine_Record
|
* @return object instance of Doctrine_Record
|
||||||
*/
|
*/
|
||||||
public function setRecord(&$record)
|
public function getRecord()
|
||||||
{
|
{
|
||||||
$this->record = $record;
|
return $this->record;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* getter for record attribute
|
* convenience function for getIterator
|
||||||
*
|
*
|
||||||
* @return object instance of Doctrine_Record
|
* @param string $type type of iterator (Pre | Post | Level)
|
||||||
*/
|
* @param array $options options
|
||||||
public function getRecord() {
|
*/
|
||||||
return $this->record;
|
public function traverse($type = 'Pre', $options = array())
|
||||||
}
|
{
|
||||||
|
return $this->getIterator($type, $options);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* convenience function for getIterator
|
* get iterator
|
||||||
*
|
*
|
||||||
* @param string $type type of iterator (Pre | Post | Level)
|
* @param string $type type of iterator (Pre | Post | Level)
|
||||||
* @param array $options options
|
* @param array $options options
|
||||||
*/
|
*/
|
||||||
public function traverse($type = 'Pre', $options = array()) {
|
public function getIterator($type = null, $options = null)
|
||||||
return $this->getIterator($type, $options);
|
{
|
||||||
}
|
if ($type === null) {
|
||||||
|
$type = (isset($this->iteratorType) ? $this->iteratorType : 'Pre');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
if ($options === null) {
|
||||||
* get iterator
|
$options = (isset($this->iteratorOptions) ? $this->iteratorOptions : array());
|
||||||
*
|
}
|
||||||
* @param string $type type of iterator (Pre | Post | Level)
|
|
||||||
* @param array $options options
|
|
||||||
*/
|
|
||||||
public function getIterator($type = null, $options = null)
|
|
||||||
{
|
|
||||||
if ($type === null)
|
|
||||||
$type = (isset($this->iteratorType) ? $this->iteratorType : 'Pre');
|
|
||||||
|
|
||||||
if ($options === null)
|
|
||||||
$options = (isset($this->iteratorOptions) ? $this->iteratorOptions : array());
|
|
||||||
|
|
||||||
$iteratorClass = 'Doctrine_Node_'.$this->record->getTable()->getTreeImplName().'_'.ucfirst(strtolower($type)).'OrderIterator';
|
|
||||||
|
|
||||||
return new $iteratorClass($this->record, $options);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
$implName = $this->record->getTable()->getTreeImplName();
|
||||||
* sets node's iterator type
|
$iteratorClass = 'Doctrine_Node_' . $implName . '_' . ucfirst(strtolower($type)) . 'OrderIterator';
|
||||||
*
|
|
||||||
* @param int
|
|
||||||
*/
|
|
||||||
public function setIteratorType($type) {
|
|
||||||
$this->iteratorType = $type;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
return new $iteratorClass($this->record, $options);
|
||||||
* sets node's iterator options
|
}
|
||||||
*
|
|
||||||
* @param int
|
/**
|
||||||
*/
|
* sets node's iterator type
|
||||||
public function setIteratorOptions($options) {
|
*
|
||||||
$this->iteratorOptions = $options;
|
* @param int
|
||||||
}
|
*/
|
||||||
} // END class
|
public function setIteratorType($type)
|
||||||
|
{
|
||||||
|
$this->iteratorType = $type;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* sets node's iterator options
|
||||||
|
*
|
||||||
|
* @param int
|
||||||
|
*/
|
||||||
|
public function setIteratorOptions($options)
|
||||||
|
{
|
||||||
|
$this->iteratorOptions = $options;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -29,5 +29,6 @@
|
|||||||
* @version $Revision$
|
* @version $Revision$
|
||||||
* @author Joe Simms <joe.simms@websites4.com>
|
* @author Joe Simms <joe.simms@websites4.com>
|
||||||
*/
|
*/
|
||||||
class Doctrine_Node_AdjacencyList extends Doctrine_Node implements Doctrine_Node_Interface { }
|
class Doctrine_Node_AdjacencyList extends Doctrine_Node implements Doctrine_Node_Interface
|
||||||
|
{}
|
||||||
|
|
||||||
|
@ -29,4 +29,5 @@
|
|||||||
* @version $Revision$
|
* @version $Revision$
|
||||||
* @author Joe Simms <joe.simms@websites4.com>
|
* @author Joe Simms <joe.simms@websites4.com>
|
||||||
*/
|
*/
|
||||||
class Doctrine_Node_AdjacencyList_LevelOrderIterator implements Iterator {}
|
class Doctrine_Node_AdjacencyList_LevelOrderIterator implements Iterator
|
||||||
|
{}
|
||||||
|
@ -29,4 +29,5 @@
|
|||||||
* @version $Revision$
|
* @version $Revision$
|
||||||
* @author Joe Simms <joe.simms@websites4.com>
|
* @author Joe Simms <joe.simms@websites4.com>
|
||||||
*/
|
*/
|
||||||
class Doctrine_Node_AdjacencyList_PostOrderIterator implements Iterator {}
|
class Doctrine_Node_AdjacencyList_PostOrderIterator implements Iterator
|
||||||
|
{}
|
||||||
|
@ -29,4 +29,5 @@
|
|||||||
* @version $Revision$
|
* @version $Revision$
|
||||||
* @author Joe Simms <joe.simms@websites4.com>
|
* @author Joe Simms <joe.simms@websites4.com>
|
||||||
*/
|
*/
|
||||||
class Doctrine_Node_AdjacencyList_PreOrderIterator implements Iterator {}
|
class Doctrine_Node_AdjacencyList_PreOrderIterator implements Iterator
|
||||||
|
{}
|
||||||
|
@ -29,4 +29,5 @@
|
|||||||
* @version $Revision$
|
* @version $Revision$
|
||||||
* @author Joe Simms <joe.simms@websites4.com>
|
* @author Joe Simms <joe.simms@websites4.com>
|
||||||
*/
|
*/
|
||||||
class Doctrine_Node_Exception extends Doctrine_Exception { }
|
class Doctrine_Node_Exception extends Doctrine_Exception
|
||||||
|
{}
|
||||||
|
@ -31,237 +31,237 @@
|
|||||||
*/
|
*/
|
||||||
interface Doctrine_Node_Interface {
|
interface Doctrine_Node_Interface {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* test if node has previous sibling
|
* test if node has previous sibling
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function hasPrevSibling();
|
public function hasPrevSibling();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* test if node has next sibling
|
* test if node has next sibling
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function hasNextSibling();
|
public function hasNextSibling();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* test if node has children
|
* test if node has children
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function hasChildren();
|
public function hasChildren();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* test if node has parent
|
* test if node has parent
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function hasParent();
|
public function hasParent();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* gets record of prev sibling or empty record
|
* gets record of prev sibling or empty record
|
||||||
*
|
*
|
||||||
* @return object Doctrine_Record
|
* @return object Doctrine_Record
|
||||||
*/
|
*/
|
||||||
public function getPrevSibling();
|
public function getPrevSibling();
|
||||||
|
|
||||||
/**
|
|
||||||
* gets record of next sibling or empty record
|
|
||||||
*
|
|
||||||
* @return object Doctrine_Record
|
|
||||||
*/
|
|
||||||
public function getNextSibling();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* gets siblings for node
|
|
||||||
*
|
|
||||||
* @return array array of sibling Doctrine_Record objects
|
|
||||||
*/
|
|
||||||
public function getSiblings($includeNode = false);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* gets record of first child or empty record
|
* gets record of next sibling or empty record
|
||||||
*
|
*
|
||||||
* @return object Doctrine_Record
|
* @return object Doctrine_Record
|
||||||
*/
|
*/
|
||||||
public function getFirstChild();
|
public function getNextSibling();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* gets record of last child or empty record
|
* gets siblings for node
|
||||||
*
|
*
|
||||||
* @return object Doctrine_Record
|
* @return array array of sibling Doctrine_Record objects
|
||||||
*/
|
*/
|
||||||
public function getLastChild();
|
public function getSiblings($includeNode = false);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* gets children for node (direct descendants only)
|
* gets record of first child or empty record
|
||||||
*
|
*
|
||||||
* @return array array of sibling Doctrine_Record objects
|
* @return object Doctrine_Record
|
||||||
*/
|
*/
|
||||||
public function getChildren();
|
public function getFirstChild();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* gets descendants for node (direct descendants only)
|
* gets record of last child or empty record
|
||||||
*
|
*
|
||||||
* @return iterator iterator to traverse descendants from node
|
* @return object Doctrine_Record
|
||||||
*/
|
*/
|
||||||
public function getDescendants();
|
public function getLastChild();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* gets record of parent or empty record
|
* gets children for node (direct descendants only)
|
||||||
*
|
*
|
||||||
* @return object Doctrine_Record
|
* @return array array of sibling Doctrine_Record objects
|
||||||
*/
|
*/
|
||||||
public function getParent();
|
public function getChildren();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* gets ancestors for node
|
* gets descendants for node (direct descendants only)
|
||||||
*
|
*
|
||||||
* @return object Doctrine_Collection
|
* @return iterator iterator to traverse descendants from node
|
||||||
*/
|
*/
|
||||||
public function getAncestors();
|
public function getDescendants();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* gets path to node from root, uses record::toString() method to get node names
|
* gets record of parent or empty record
|
||||||
*
|
*
|
||||||
* @param string $seperator path seperator
|
* @return object Doctrine_Record
|
||||||
* @param bool $includeNode whether or not to include node at end of path
|
*/
|
||||||
* @return string string representation of path
|
public function getParent();
|
||||||
*/
|
|
||||||
public function getPath($seperator = ' > ', $includeNode = false);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* gets level (depth) of node in the tree
|
* gets ancestors for node
|
||||||
*
|
*
|
||||||
* @return int
|
* @return object Doctrine_Collection
|
||||||
*/
|
*/
|
||||||
public function getLevel();
|
public function getAncestors();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* gets number of children (direct descendants)
|
* gets path to node from root, uses record::toString() method to get node names
|
||||||
*
|
*
|
||||||
* @return int
|
* @param string $seperator path seperator
|
||||||
*/
|
* @param bool $includeNode whether or not to include node at end of path
|
||||||
public function getNumberChildren();
|
* @return string string representation of path
|
||||||
|
*/
|
||||||
|
public function getPath($seperator = ' > ', $includeNode = false);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* gets number of descendants (children and their children)
|
* gets level (depth) of node in the tree
|
||||||
*
|
*
|
||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
public function getNumberDescendants();
|
public function getLevel();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* inserts node as parent of dest record
|
* gets number of children (direct descendants)
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return int
|
||||||
*/
|
*/
|
||||||
public function insertAsParentOf(Doctrine_Record $dest);
|
public function getNumberChildren();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* inserts node as previous sibling of dest record
|
* gets number of descendants (children and their children)
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return int
|
||||||
*/
|
*/
|
||||||
public function insertAsPrevSiblingOf(Doctrine_Record $dest);
|
public function getNumberDescendants();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* inserts node as next sibling of dest record
|
* inserts node as parent of dest record
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function insertAsNextSiblingOf(Doctrine_Record $dest);
|
public function insertAsParentOf(Doctrine_Record $dest);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* inserts node as first child of dest record
|
* inserts node as previous sibling of dest record
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function insertAsFirstChildOf(Doctrine_Record $dest);
|
public function insertAsPrevSiblingOf(Doctrine_Record $dest);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* inserts node as first child of dest record
|
* inserts node as next sibling of dest record
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function insertAsLastChildOf(Doctrine_Record $dest);
|
public function insertAsNextSiblingOf(Doctrine_Record $dest);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* moves node as prev sibling of dest record
|
* inserts node as first child of dest record
|
||||||
*
|
*
|
||||||
*/
|
* @return bool
|
||||||
public function moveAsPrevSiblingOf(Doctrine_Record $dest);
|
*/
|
||||||
|
public function insertAsFirstChildOf(Doctrine_Record $dest);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* moves node as next sibling of dest record
|
* inserts node as first child of dest record
|
||||||
*
|
*
|
||||||
*/
|
* @return bool
|
||||||
public function moveAsNextSiblingOf(Doctrine_Record $dest);
|
*/
|
||||||
|
public function insertAsLastChildOf(Doctrine_Record $dest);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* moves node as first child of dest record
|
* moves node as prev sibling of dest record
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public function moveAsFirstChildOf(Doctrine_Record $dest);
|
public function moveAsPrevSiblingOf(Doctrine_Record $dest);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* moves node as last child of dest record
|
* moves node as next sibling of dest record
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public function moveAsLastChildOf(Doctrine_Record $dest);
|
public function moveAsNextSiblingOf(Doctrine_Record $dest);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* adds node as last child of record
|
* moves node as first child of dest record
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public function addChild(Doctrine_Record $record);
|
public function moveAsFirstChildOf(Doctrine_Record $dest);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* determines if node is leaf
|
* moves node as last child of dest record
|
||||||
*
|
*
|
||||||
* @return bool
|
*/
|
||||||
*/
|
public function moveAsLastChildOf(Doctrine_Record $dest);
|
||||||
public function isLeaf();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* determines if node is root
|
* adds node as last child of record
|
||||||
*
|
*
|
||||||
* @return bool
|
*/
|
||||||
*/
|
public function addChild(Doctrine_Record $record);
|
||||||
public function isRoot();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* determines if node is equal to subject node
|
* determines if node is leaf
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function isEqualTo(Doctrine_Record $subj);
|
public function isLeaf();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* determines if node is child of subject node
|
* determines if node is root
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function isDescendantOf(Doctrine_Record $subj);
|
public function isRoot();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* determines if node is child of or sibling to subject node
|
* determines if node is equal to subject node
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function isDescendantOfOrEqualTo(Doctrine_Record $subj);
|
public function isEqualTo(Doctrine_Record $subj);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* determines if node is valid
|
* determines if node is child of subject node
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function isValidNode();
|
public function isDescendantOf(Doctrine_Record $subj);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* deletes node and it's descendants
|
* determines if node is child of or sibling to subject node
|
||||||
*
|
*
|
||||||
*/
|
* @return bool
|
||||||
public function delete();
|
*/
|
||||||
}
|
public function isDescendantOfOrEqualTo(Doctrine_Record $subj);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* determines if node is valid
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function isValidNode();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* deletes node and it's descendants
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public function delete();
|
||||||
|
}
|
||||||
|
@ -29,5 +29,6 @@
|
|||||||
* @version $Revision$
|
* @version $Revision$
|
||||||
* @author Joe Simms <joe.simms@websites4.com>
|
* @author Joe Simms <joe.simms@websites4.com>
|
||||||
*/
|
*/
|
||||||
class Doctrine_Node_MaterializedPath extends Doctrine_Node implements Doctrine_Node_Interface { }
|
class Doctrine_Node_MaterializedPath extends Doctrine_Node implements Doctrine_Node_Interface
|
||||||
|
{}
|
||||||
|
|
||||||
|
@ -32,30 +32,36 @@
|
|||||||
class Doctrine_Node_MaterializedPath_LevelOrderIterator implements Iterator
|
class Doctrine_Node_MaterializedPath_LevelOrderIterator implements Iterator
|
||||||
{
|
{
|
||||||
private $topNode = null;
|
private $topNode = null;
|
||||||
|
|
||||||
private $curNode = null;
|
private $curNode = null;
|
||||||
|
|
||||||
public function __construct($node, $opts) {
|
public function __construct($node, $opts)
|
||||||
throw new Doctrine_Exception('Not yet implemented');
|
{
|
||||||
}
|
|
||||||
|
|
||||||
public function rewind() {
|
|
||||||
throw new Doctrine_Exception('Not yet implemented');
|
|
||||||
}
|
|
||||||
|
|
||||||
public function valid() {
|
|
||||||
throw new Doctrine_Exception('Not yet implemented');
|
|
||||||
}
|
|
||||||
|
|
||||||
public function current() {
|
|
||||||
throw new Doctrine_Exception('Not yet implemented');
|
|
||||||
}
|
|
||||||
|
|
||||||
public function key() {
|
|
||||||
throw new Doctrine_Exception('Not yet implemented');
|
throw new Doctrine_Exception('Not yet implemented');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function next() {
|
public function rewind()
|
||||||
|
{
|
||||||
throw new Doctrine_Exception('Not yet implemented');
|
throw new Doctrine_Exception('Not yet implemented');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function valid()
|
||||||
|
{
|
||||||
|
throw new Doctrine_Exception('Not yet implemented');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function current()
|
||||||
|
{
|
||||||
|
throw new Doctrine_Exception('Not yet implemented');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function key()
|
||||||
|
{
|
||||||
|
throw new Doctrine_Exception('Not yet implemented');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function next()
|
||||||
|
{
|
||||||
|
throw new Doctrine_Exception('Not yet implemented');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -32,30 +32,36 @@
|
|||||||
class Doctrine_Node_MaterializedPath_PostOrderIterator implements Iterator
|
class Doctrine_Node_MaterializedPath_PostOrderIterator implements Iterator
|
||||||
{
|
{
|
||||||
private $topNode = null;
|
private $topNode = null;
|
||||||
|
|
||||||
private $curNode = null;
|
private $curNode = null;
|
||||||
|
|
||||||
public function __construct($node, $opts) {
|
public function __construct($node, $opts)
|
||||||
throw new Doctrine_Exception('Not yet implemented');
|
{
|
||||||
}
|
|
||||||
|
|
||||||
public function rewind() {
|
|
||||||
throw new Doctrine_Exception('Not yet implemented');
|
|
||||||
}
|
|
||||||
|
|
||||||
public function valid() {
|
|
||||||
throw new Doctrine_Exception('Not yet implemented');
|
|
||||||
}
|
|
||||||
|
|
||||||
public function current() {
|
|
||||||
throw new Doctrine_Exception('Not yet implemented');
|
|
||||||
}
|
|
||||||
|
|
||||||
public function key() {
|
|
||||||
throw new Doctrine_Exception('Not yet implemented');
|
throw new Doctrine_Exception('Not yet implemented');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function next() {
|
public function rewind()
|
||||||
|
{
|
||||||
throw new Doctrine_Exception('Not yet implemented');
|
throw new Doctrine_Exception('Not yet implemented');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function valid()
|
||||||
|
{
|
||||||
|
throw new Doctrine_Exception('Not yet implemented');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function current()
|
||||||
|
{
|
||||||
|
throw new Doctrine_Exception('Not yet implemented');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function key()
|
||||||
|
{
|
||||||
|
throw new Doctrine_Exception('Not yet implemented');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function next()
|
||||||
|
{
|
||||||
|
throw new Doctrine_Exception('Not yet implemented');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -32,30 +32,36 @@
|
|||||||
class Doctrine_Node_MaterializedPath_PreOrderIterator implements Iterator
|
class Doctrine_Node_MaterializedPath_PreOrderIterator implements Iterator
|
||||||
{
|
{
|
||||||
private $topNode = null;
|
private $topNode = null;
|
||||||
|
|
||||||
private $curNode = null;
|
private $curNode = null;
|
||||||
|
|
||||||
public function __construct($node, $opts) {
|
public function __construct($node, $opts)
|
||||||
throw new Doctrine_Exception('Not yet implemented');
|
{
|
||||||
}
|
|
||||||
|
|
||||||
public function rewind() {
|
|
||||||
throw new Doctrine_Exception('Not yet implemented');
|
|
||||||
}
|
|
||||||
|
|
||||||
public function valid() {
|
|
||||||
throw new Doctrine_Exception('Not yet implemented');
|
|
||||||
}
|
|
||||||
|
|
||||||
public function current() {
|
|
||||||
throw new Doctrine_Exception('Not yet implemented');
|
|
||||||
}
|
|
||||||
|
|
||||||
public function key() {
|
|
||||||
throw new Doctrine_Exception('Not yet implemented');
|
throw new Doctrine_Exception('Not yet implemented');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function next() {
|
public function rewind()
|
||||||
|
{
|
||||||
throw new Doctrine_Exception('Not yet implemented');
|
throw new Doctrine_Exception('Not yet implemented');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function valid()
|
||||||
|
{
|
||||||
|
throw new Doctrine_Exception('Not yet implemented');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function current()
|
||||||
|
{
|
||||||
|
throw new Doctrine_Exception('Not yet implemented');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function key()
|
||||||
|
{
|
||||||
|
throw new Doctrine_Exception('Not yet implemented');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function next()
|
||||||
|
{
|
||||||
|
throw new Doctrine_Exception('Not yet implemented');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -29,4 +29,5 @@
|
|||||||
* @version $Revision$
|
* @version $Revision$
|
||||||
* @author Joe Simms <joe.simms@websites4.com>
|
* @author Joe Simms <joe.simms@websites4.com>
|
||||||
*/
|
*/
|
||||||
class Doctrine_Node_NestedSet_LevelOrderIterator implements Iterator {}
|
class Doctrine_Node_NestedSet_LevelOrderIterator implements Iterator
|
||||||
|
{}
|
||||||
|
@ -29,4 +29,5 @@
|
|||||||
* @version $Revision$
|
* @version $Revision$
|
||||||
* @author Joe Simms <joe.simms@websites4.com>
|
* @author Joe Simms <joe.simms@websites4.com>
|
||||||
*/
|
*/
|
||||||
class Doctrine_Node_NestedSet_PostOrderIterator implements Iterator {}
|
class Doctrine_Node_NestedSet_PostOrderIterator implements Iterator
|
||||||
|
{}
|
||||||
|
@ -30,9 +30,9 @@
|
|||||||
* @author Joe Simms <joe.simms@websites4.com>
|
* @author Joe Simms <joe.simms@websites4.com>
|
||||||
*/
|
*/
|
||||||
class Doctrine_Node_NestedSet_PreOrderIterator implements Iterator
|
class Doctrine_Node_NestedSet_PreOrderIterator implements Iterator
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var Doctrine_Collection $collection
|
* @var Doctrine_Collection $collection
|
||||||
*/
|
*/
|
||||||
protected $collection;
|
protected $collection;
|
||||||
/**
|
/**
|
||||||
@ -59,34 +59,32 @@ class Doctrine_Node_NestedSet_PreOrderIterator implements Iterator
|
|||||||
* @var integer $count
|
* @var integer $count
|
||||||
*/
|
*/
|
||||||
protected $count;
|
protected $count;
|
||||||
|
|
||||||
public function __construct($record, $opts) {
|
|
||||||
|
|
||||||
$componentName = $record->getTable()->getComponentName();
|
public function __construct($record, $opts)
|
||||||
|
{
|
||||||
$q = $record->getTable()->createQuery();
|
$componentName = $record->getTable()->getComponentName();
|
||||||
|
|
||||||
if(isset($opts['include_record']) && $opts['include_record'])
|
$q = $record->getTable()->createQuery();
|
||||||
{
|
|
||||||
$query = $q->where("$componentName.lft >= ? AND $componentName.rgt <= ?", array($record->get('lft'), $record->get('rgt')))->orderBy('lft asc');
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
$query = $q->where("$componentName.lft > ? AND $componentName.rgt < ?", array($record->get('lft'), $record->get('rgt')))->orderBy('lft asc');
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->maxLevel = isset($opts['depth']) ? ($opts['depth'] + $record->getNode()->getLevel()) : 0;
|
$params = array($record->get('lft'), $record->get('rgt'));
|
||||||
$this->options = $opts;
|
if (isset($opts['include_record']) && $opts['include_record']) {
|
||||||
$this->collection = isset($opts['collection']) ? $opts['collection'] : $query->execute();
|
$query = $q->where("$componentName.lft >= ? AND $componentName.rgt <= ?", $params)->orderBy('lft asc');
|
||||||
$this->keys = $this->collection->getKeys();
|
} else {
|
||||||
$this->count = $this->collection->count();
|
$query = $q->where("$componentName.lft > ? AND $componentName.rgt < ?", $params)->orderBy('lft asc');
|
||||||
$this->index = -1;
|
}
|
||||||
$this->level = $record->getNode()->getLevel();
|
|
||||||
$this->prevLeft = $record->getNode()->getLeftValue();
|
$this->maxLevel = isset($opts['depth']) ? ($opts['depth'] + $record->getNode()->getLevel()) : 0;
|
||||||
|
$this->options = $opts;
|
||||||
echo $this->maxDepth;
|
$this->collection = isset($opts['collection']) ? $opts['collection'] : $query->execute();
|
||||||
// clear the table identity cache
|
$this->keys = $this->collection->getKeys();
|
||||||
$record->getTable()->clear();
|
$this->count = $this->collection->count();
|
||||||
|
$this->index = -1;
|
||||||
|
$this->level = $record->getNode()->getLevel();
|
||||||
|
$this->prevLeft = $record->getNode()->getLeftValue();
|
||||||
|
|
||||||
|
echo $this->maxDepth;
|
||||||
|
// clear the table identity cache
|
||||||
|
$record->getTable()->clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -94,7 +92,8 @@ class Doctrine_Node_NestedSet_PreOrderIterator implements Iterator
|
|||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function rewind() {
|
public function rewind()
|
||||||
|
{
|
||||||
$this->index = -1;
|
$this->index = -1;
|
||||||
$this->key = null;
|
$this->key = null;
|
||||||
}
|
}
|
||||||
@ -104,66 +103,73 @@ class Doctrine_Node_NestedSet_PreOrderIterator implements Iterator
|
|||||||
*
|
*
|
||||||
* @return integer
|
* @return integer
|
||||||
*/
|
*/
|
||||||
public function key() {
|
public function key()
|
||||||
|
{
|
||||||
return $this->key;
|
return $this->key;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* returns the current record
|
* returns the current record
|
||||||
*
|
*
|
||||||
* @return Doctrine_Record
|
* @return Doctrine_Record
|
||||||
*/
|
*/
|
||||||
public function current() {
|
public function current()
|
||||||
|
{
|
||||||
$record = $this->collection->get($this->key);
|
$record = $this->collection->get($this->key);
|
||||||
$record->getNode()->setLevel($this->level);
|
$record->getNode()->setLevel($this->level);
|
||||||
return $record;
|
return $record;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* advances the internal pointer
|
* advances the internal pointer
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function next() {
|
public function next()
|
||||||
while($current = $this->advanceIndex())
|
{
|
||||||
{
|
while ($current = $this->advanceIndex()) {
|
||||||
if($this->maxLevel && ($this->level > $this->maxLevel))
|
if ($this->maxLevel && ($this->level > $this->maxLevel)) {
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
return $current;
|
|
||||||
}
|
return $current;
|
||||||
|
}
|
||||||
return false;
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return boolean whether or not the iteration will continue
|
* @return boolean whether or not the iteration will continue
|
||||||
*/
|
*/
|
||||||
public function valid() {
|
public function valid()
|
||||||
|
{
|
||||||
return ($this->index < $this->count);
|
return ($this->index < $this->count);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function count() {
|
public function count()
|
||||||
|
{
|
||||||
return $this->count;
|
return $this->count;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function updateLevel() {
|
private function updateLevel()
|
||||||
if(!(isset($this->options['include_record']) && $this->options['include_record'] && $this->index == 0))
|
{
|
||||||
{
|
if (!(isset($this->options['include_record']) && $this->options['include_record'] && $this->index == 0)) {
|
||||||
$left = $this->collection->get($this->key)->getNode()->getLeftValue();
|
$left = $this->collection->get($this->key)->getNode()->getLeftValue();
|
||||||
$this->level += $this->prevLeft - $left + 2;
|
$this->level += $this->prevLeft - $left + 2;
|
||||||
$this->prevLeft = $left;
|
$this->prevLeft = $left;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function advanceIndex() {
|
private function advanceIndex()
|
||||||
|
{
|
||||||
$this->index++;
|
$this->index++;
|
||||||
$i = $this->index;
|
$i = $this->index;
|
||||||
if(isset($this->keys[$i]))
|
if (isset($this->keys[$i])) {
|
||||||
{
|
|
||||||
$this->key = $this->keys[$i];
|
$this->key = $this->keys[$i];
|
||||||
$this->updateLevel();
|
$this->updateLevel();
|
||||||
return $this->current();
|
return $this->current();
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
108
draft/Tree.php
108
draft/Tree.php
@ -29,61 +29,63 @@
|
|||||||
* @version $Revision$
|
* @version $Revision$
|
||||||
* @author Joe Simms <joe.simms@websites4.com>
|
* @author Joe Simms <joe.simms@websites4.com>
|
||||||
*/
|
*/
|
||||||
class Doctrine_Tree
|
class Doctrine_Tree
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @param object $table reference to associated Doctrine_Table instance
|
* @param object $table reference to associated Doctrine_Table instance
|
||||||
*/
|
*/
|
||||||
protected $table;
|
protected $table;
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array $options
|
|
||||||
*/
|
|
||||||
protected $options;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* contructor, creates tree with reference to table and any options
|
* @param array $options
|
||||||
*
|
*/
|
||||||
* @param object $table instance of Doctrine_Table
|
protected $options;
|
||||||
* @param array $options options
|
|
||||||
*/
|
|
||||||
public function __construct($table, $options)
|
|
||||||
{
|
|
||||||
$this->table = $table;
|
|
||||||
$this->options = $options;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Used to define table attributes required for the given implementation
|
* constructor, creates tree with reference to table and any options
|
||||||
*
|
*
|
||||||
*/
|
* @param object $table instance of Doctrine_Table
|
||||||
public function setTableDefinition()
|
* @param array $options options
|
||||||
{
|
*/
|
||||||
throw new Doctrine_Tree_Exception('Table attributes have not been defined for this Tree implementation.');
|
public function __construct($table, $options)
|
||||||
}
|
{
|
||||||
|
$this->table = $table;
|
||||||
|
$this->options = $options;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* this method is used for setting up relations and attributes and should be used by specific implementations
|
* Used to define table attributes required for the given implementation
|
||||||
*
|
*
|
||||||
*/
|
* @throws Doctrine_Tree_Exception if table attributes have not been defined
|
||||||
public function setUp()
|
*/
|
||||||
{
|
public function setTableDefinition()
|
||||||
|
{
|
||||||
}
|
throw new Doctrine_Tree_Exception('Table attributes have not been defined for this Tree implementation.');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* factory method to return tree instance based upon chosen implementation
|
* this method is used for setting up relations and attributes and should be used by specific implementations
|
||||||
*
|
*
|
||||||
* @param object $table instance of Doctrine_Table
|
*/
|
||||||
* @param string $impName implementation (NestedSet, AdjacencyList, MaterializedPath)
|
public function setUp()
|
||||||
* @param array $options options
|
{
|
||||||
* @return object $options instance of Doctrine_Node
|
}
|
||||||
*/
|
|
||||||
public static function factory( &$table, $implName, $options = array())
|
/**
|
||||||
{
|
* factory method to return tree instance based upon chosen implementation
|
||||||
$class = 'Doctrine_Tree_'.$implName;
|
*
|
||||||
if(!class_exists($class))
|
* @param object $table instance of Doctrine_Table
|
||||||
throw new Doctrine_Exception('The chosen class must extend Doctrine_Tree');
|
* @param string $impName implementation (NestedSet, AdjacencyList, MaterializedPath)
|
||||||
return new $class($table, $options);
|
* @param array $options options
|
||||||
}
|
* @return object $options instance of Doctrine_Node
|
||||||
} // END class
|
* @throws Doctrine_Exception if class does not extend Doctrine_Tree
|
||||||
|
*/
|
||||||
|
public static function factory(&$table, $implName, $options = array())
|
||||||
|
{
|
||||||
|
$class = 'Doctrine_Tree_' . $implName;
|
||||||
|
if (!class_exists($class)) {
|
||||||
|
throw new Doctrine_Exception('The chosen class must extend Doctrine_Tree');
|
||||||
|
}
|
||||||
|
return new $class($table, $options);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -29,4 +29,5 @@
|
|||||||
* @version $Revision$
|
* @version $Revision$
|
||||||
* @author Joe Simms <joe.simms@websites4.com>
|
* @author Joe Simms <joe.simms@websites4.com>
|
||||||
*/
|
*/
|
||||||
class Doctrine_Tree_AdjacencyList extends Doctrine_Tree implements Doctrine_Tree_Interface { }
|
class Doctrine_Tree_AdjacencyList extends Doctrine_Tree implements Doctrine_Tree_Interface
|
||||||
|
{}
|
||||||
|
@ -29,4 +29,5 @@
|
|||||||
* @version $Revision$
|
* @version $Revision$
|
||||||
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
||||||
*/
|
*/
|
||||||
class Doctrine_Tree_Exception extends Doctrine_Exception { }
|
class Doctrine_Tree_Exception extends Doctrine_Exception
|
||||||
|
{}
|
||||||
|
@ -31,34 +31,34 @@
|
|||||||
*/
|
*/
|
||||||
interface Doctrine_Tree_Interface {
|
interface Doctrine_Tree_Interface {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* creates root node from given record or from a new record
|
* creates root node from given record or from a new record
|
||||||
*
|
*
|
||||||
* @param object $record instance of Doctrine_Record
|
* @param object $record instance of Doctrine_Record
|
||||||
*/
|
*/
|
||||||
public function createRoot(Doctrine_Record $record = null);
|
public function createRoot(Doctrine_Record $record = null);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* returns root node
|
* returns root node
|
||||||
*
|
*
|
||||||
* @return object $record instance of Doctrine_Record
|
* @return object $record instance of Doctrine_Record
|
||||||
*/
|
*/
|
||||||
public function findRoot();
|
public function findRoot();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* optimised method to returns iterator for traversal of the entire tree from root
|
* optimised method to returns iterator for traversal of the entire tree from root
|
||||||
*
|
*
|
||||||
* @param array $options options
|
* @param array $options options
|
||||||
* @return object $iterator instance of Doctrine_Node_<Implementation>_PreOrderIterator
|
* @return object $iterator instance of Doctrine_Node_<Implementation>_PreOrderIterator
|
||||||
*/
|
*/
|
||||||
public function fetchTree($options = array());
|
public function fetchTree($options = array());
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* optimised method that returns iterator for traversal of the tree from the given record primary key
|
* optimised method that returns iterator for traversal of the tree from the given record primary key
|
||||||
*
|
*
|
||||||
* @param mixed $pk primary key as used by table::find() to locate node to traverse tree from
|
* @param mixed $pk primary key as used by table::find() to locate node to traverse tree from
|
||||||
* @param array $options options
|
* @param array $options options
|
||||||
* @return iterator instance of Doctrine_Node_<Implementation>_PreOrderIterator
|
* @return iterator instance of Doctrine_Node_<Implementation>_PreOrderIterator
|
||||||
*/
|
*/
|
||||||
public function fetchBranch($pk, $options = array());
|
public function fetchBranch($pk, $options = array());
|
||||||
}
|
}
|
||||||
|
@ -29,4 +29,5 @@
|
|||||||
* @version $Revision$
|
* @version $Revision$
|
||||||
* @author Joe Simms <joe.simms@websites4.com>
|
* @author Joe Simms <joe.simms@websites4.com>
|
||||||
*/
|
*/
|
||||||
class Doctrine_Tree_MaterializedPath extends Doctrine_Tree implements Doctrine_Tree_Interface { }
|
class Doctrine_Tree_MaterializedPath extends Doctrine_Tree implements Doctrine_Tree_Interface
|
||||||
|
{}
|
||||||
|
@ -31,113 +31,114 @@
|
|||||||
*/
|
*/
|
||||||
class Doctrine_Tree_NestedSet extends Doctrine_Tree implements Doctrine_Tree_Interface
|
class Doctrine_Tree_NestedSet extends Doctrine_Tree implements Doctrine_Tree_Interface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* used to define table attributes required for the NestetSet implementation
|
* used to define table attributes required for the NestetSet implementation
|
||||||
* adds lft and rgt columns for corresponding left and right values
|
* adds lft and rgt columns for corresponding left and right values
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public function setTableDefinition()
|
public function setTableDefinition()
|
||||||
{
|
|
||||||
$this->table->setColumn("lft","integer",11);
|
|
||||||
$this->table->setColumn("rgt","integer",11);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* creates root node from given record or from a new record
|
|
||||||
*
|
|
||||||
* @param object $record instance of Doctrine_Record
|
|
||||||
*/
|
|
||||||
public function createRoot(Doctrine_Record $record = null)
|
|
||||||
{
|
|
||||||
if(!$record)
|
|
||||||
{
|
{
|
||||||
$record = $this->table->create();
|
$this->table->setColumn("lft","integer",11);
|
||||||
}
|
$this->table->setColumn("rgt","integer",11);
|
||||||
|
|
||||||
$record->set('lft', '1');
|
|
||||||
$record->set('rgt', '2');
|
|
||||||
$record->save();
|
|
||||||
|
|
||||||
return $record;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* returns root node
|
|
||||||
*
|
|
||||||
* @return object $record instance of Doctrine_Record
|
|
||||||
*/
|
|
||||||
public function findRoot()
|
|
||||||
{
|
|
||||||
$q = $this->table->createQuery();
|
|
||||||
$root = $q->where('lft = ?', 1)
|
|
||||||
->execute()->getFirst();
|
|
||||||
|
|
||||||
// if no record is returned, create record
|
|
||||||
if(!$root)
|
|
||||||
{
|
|
||||||
$root = $this->table->create();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// set level to prevent additional query to determine level
|
/**
|
||||||
$root->getNode()->setLevel(0);
|
* creates root node from given record or from a new record
|
||||||
|
*
|
||||||
return $root;
|
* @param object $record instance of Doctrine_Record
|
||||||
}
|
*/
|
||||||
/**
|
public function createRoot(Doctrine_Record $record = null)
|
||||||
* optimised method to returns iterator for traversal of the entire tree from root
|
|
||||||
*
|
|
||||||
* @param array $options options
|
|
||||||
* @return object $iterator instance of Doctrine_Node_NestedSet_PreOrderIterator
|
|
||||||
*/
|
|
||||||
public function fetchTree($options = array())
|
|
||||||
{
|
|
||||||
// fetch tree
|
|
||||||
$q = $this->table->createQuery();
|
|
||||||
|
|
||||||
$tree = $q->where('lft >= ?', 1)
|
|
||||||
->orderBy('lft asc')
|
|
||||||
->execute();
|
|
||||||
|
|
||||||
$root = $tree->getFirst();
|
|
||||||
|
|
||||||
// if no record is returned, create record
|
|
||||||
if(!$root)
|
|
||||||
{
|
{
|
||||||
$root = $this->table->create();
|
if (!$record) {
|
||||||
|
$record = $this->table->create();
|
||||||
|
}
|
||||||
|
|
||||||
|
$record->set('lft', '1');
|
||||||
|
$record->set('rgt', '2');
|
||||||
|
$record->save();
|
||||||
|
|
||||||
|
return $record;
|
||||||
}
|
}
|
||||||
|
|
||||||
if($root->exists())
|
/**
|
||||||
|
* returns root node
|
||||||
|
*
|
||||||
|
* @return object $record instance of Doctrine_Record
|
||||||
|
*/
|
||||||
|
public function findRoot()
|
||||||
{
|
{
|
||||||
// set level to prevent additional query
|
$q = $this->table->createQuery();
|
||||||
$root->getNode()->setLevel(0);
|
$root = $q->where('lft = ?', 1)
|
||||||
|
->execute()->getFirst();
|
||||||
|
|
||||||
// default to include root node
|
// if no record is returned, create record
|
||||||
$options = array_merge(array('include_record'=>true), $options);
|
if (!$root) {
|
||||||
|
$root = $this->table->create();
|
||||||
// remove root node from collection if not required
|
}
|
||||||
if($options['include_record'] == false)
|
|
||||||
$tree->remove(0);
|
|
||||||
|
|
||||||
// set collection for iterator
|
// set level to prevent additional query to determine level
|
||||||
$options['collection'] = $tree;
|
$root->getNode()->setLevel(0);
|
||||||
|
|
||||||
return $root->getNode()->traverse('Pre', $options);
|
return $root;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
/**
|
/**
|
||||||
* optimised method that returns iterator for traversal of the tree from the given record primary key
|
* optimised method to returns iterator for traversal of the entire tree from root
|
||||||
*
|
*
|
||||||
* @param mixed $pk primary key as used by table::find() to locate node to traverse tree from
|
* @param array $options options
|
||||||
* @param array $options options
|
* @return object $iterator instance of Doctrine_Node_NestedSet_PreOrderIterator
|
||||||
* @return iterator instance of Doctrine_Node_<Implementation>_PreOrderIterator
|
*/
|
||||||
*/
|
public function fetchTree($options = array())
|
||||||
public function fetchBranch($pk, $options = array())
|
|
||||||
{
|
|
||||||
$record = $this->table->find($pk);
|
|
||||||
if($record->exists())
|
|
||||||
{
|
{
|
||||||
$options = array_merge(array('include_record'=>true), $options);
|
// fetch tree
|
||||||
return $record->getNode()->traverse('Pre', $options);
|
$q = $this->table->createQuery();
|
||||||
|
|
||||||
|
$tree = $q->where('lft >= ?', 1)
|
||||||
|
->orderBy('lft asc')
|
||||||
|
->execute();
|
||||||
|
|
||||||
|
$root = $tree->getFirst();
|
||||||
|
|
||||||
|
// if no record is returned, create record
|
||||||
|
if (!$root) {
|
||||||
|
$root = $this->table->create();
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($root->exists()) {
|
||||||
|
// set level to prevent additional query
|
||||||
|
$root->getNode()->setLevel(0);
|
||||||
|
|
||||||
|
// default to include root node
|
||||||
|
$options = array_merge(array('include_record'=>true), $options);
|
||||||
|
|
||||||
|
// remove root node from collection if not required
|
||||||
|
if($options['include_record'] == false)
|
||||||
|
$tree->remove(0);
|
||||||
|
|
||||||
|
// set collection for iterator
|
||||||
|
$options['collection'] = $tree;
|
||||||
|
|
||||||
|
return $root->getNode()->traverse('Pre', $options);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: no default return value or exception thrown?
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
/**
|
||||||
|
* optimised method that returns iterator for traversal of the tree from the given record primary key
|
||||||
|
*
|
||||||
|
* @param mixed $pk primary key as used by table::find() to locate node to traverse tree from
|
||||||
|
* @param array $options options
|
||||||
|
* @return iterator instance of Doctrine_Node_<Implementation>_PreOrderIterator
|
||||||
|
*/
|
||||||
|
public function fetchBranch($pk, $options = array())
|
||||||
|
{
|
||||||
|
$record = $this->table->find($pk);
|
||||||
|
if ($record->exists()) {
|
||||||
|
$options = array_merge(array('include_record'=>true), $options);
|
||||||
|
return $record->getNode()->traverse('Pre', $options);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: if record doesn't exist, throw exception or similar?
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user