joesimms: initial draft for tree support. NestedSet support included, placeholders for other popular implementations also included. Read the README.tree file for more information and changelog to core files. Modified core files have also been included in this commit. hope it works and you like it !
This commit is contained in:
parent
f56b11afbd
commit
9b53bb898b
406
draft/EXAMPLE.tree.php
Normal file
406
draft/EXAMPLE.tree.php
Normal file
@ -0,0 +1,406 @@
|
||||
<?php
|
||||
/*please note that this is a very DRAFT and basic example of how you can use the different functions available in the tree implentation*/
|
||||
|
||||
require_once("/Users/joesimms/projects/doctrine/trunk/lib/Doctrine.php");
|
||||
|
||||
// autoloading objects, modified function to search drafts folder first, should run this test script from the drafts folder
|
||||
function __autoload($classname) {
|
||||
|
||||
if (class_exists($classname)) {
|
||||
return false;
|
||||
}
|
||||
if (! $path) {
|
||||
$path = dirname(__FILE__);
|
||||
}
|
||||
$classpath = str_replace('Doctrine_', '',$classname);
|
||||
|
||||
$class = $path.DIRECTORY_SEPARATOR . str_replace('_', DIRECTORY_SEPARATOR,$classpath) . '.php';
|
||||
|
||||
if ( !file_exists($class)) {
|
||||
return Doctrine::autoload($classname);
|
||||
}
|
||||
|
||||
require_once($class);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// define our tree
|
||||
class Menu extends Doctrine_Record {
|
||||
public function setTableDefinition() {
|
||||
|
||||
$this->setTableName('menu');
|
||||
|
||||
// add this your table definition to set the table as NestedSet tree implementation
|
||||
$this->actsAsTree('NestedSet');
|
||||
|
||||
// you do not need to add any columns specific to the nested set implementation, these are added for you
|
||||
$this->hasColumn("name","string",30);
|
||||
|
||||
}
|
||||
|
||||
// this toString() function is used to get the name for the path, see node::getPath
|
||||
// maybe change to actually use __toString(), howvever, i wondered if this had any significance ??
|
||||
public function toString() {
|
||||
return $this->get('name');
|
||||
}
|
||||
}
|
||||
|
||||
// set connections to database
|
||||
$dsn = 'mysql:dbname=nestedset;host=localhost';
|
||||
$user = 'user';
|
||||
$password = 'pass';
|
||||
|
||||
try {
|
||||
$dbh = new PDO($dsn, $user, $password);
|
||||
} catch (PDOException $e) {
|
||||
echo 'Connection failed: ' . $e->getMessage();
|
||||
}
|
||||
|
||||
$manager = Doctrine_Manager::getInstance();
|
||||
|
||||
$conn = $manager->openConnection($dbh);
|
||||
|
||||
// create root
|
||||
$root = new Menu();
|
||||
$root->set('name', 'root');
|
||||
|
||||
$manager->getTable('Menu')->getTree()->createRoot($root);
|
||||
|
||||
// build tree
|
||||
$two = new Menu();
|
||||
$two->set('name', '2');
|
||||
$root->getNode()->addChild($two);
|
||||
|
||||
$one = new Menu();
|
||||
$one->set('name', '1');
|
||||
$one->getNode()->insertAsPrevSiblingOf($two);
|
||||
|
||||
// refresh as node's lft and rgt values have changed, zYne, can we automate this?
|
||||
$two->refresh();
|
||||
|
||||
$three = new Menu();
|
||||
$three->set('name', '3');
|
||||
$three->getNode()->insertAsNextSiblingOf($two);
|
||||
$two->refresh();
|
||||
|
||||
$one_one = new Menu();
|
||||
$one_one->set('name', '1.1');
|
||||
$one_one->getNode()->insertAsFirstChildOf($one);
|
||||
$one->refresh();
|
||||
|
||||
$one_two = new Menu();
|
||||
$one_two->set('name', '1.2');
|
||||
$one_two->getNode()->insertAsLastChildOf($one);
|
||||
$one_two->refresh();
|
||||
|
||||
$one_two_one = new Menu();
|
||||
$one_two_one->set('name', '1.2.1');
|
||||
$one_two->getNode()->addChild($one_two_one);
|
||||
|
||||
$root->refresh();
|
||||
$four = new Menu();
|
||||
$four->set('name', '4');
|
||||
$root->getNode()->addChild($four);
|
||||
|
||||
$root->refresh();
|
||||
$five = new Menu();
|
||||
$five->set('name', '5');
|
||||
$root->getNode()->addChild($five);
|
||||
|
||||
$root->refresh();
|
||||
$six = new Menu();
|
||||
$six->set('name', '6');
|
||||
$root->getNode()->addChild($six);
|
||||
|
||||
output_message('initial tree');
|
||||
output_tree($root);
|
||||
|
||||
$one_one->refresh();
|
||||
$six->set('name', '1.0 (was 6)');
|
||||
$six->getNode()->moveAsPrevSiblingOf($one_one);
|
||||
|
||||
$one_two->refresh();
|
||||
$five->refresh();
|
||||
$five->set('name', '1.3 (was 5)');
|
||||
$five->getNode()->moveAsNextSiblingOf($one_two);
|
||||
|
||||
$one_one->refresh();
|
||||
$four->refresh();
|
||||
$four->set('name', '1.1.1 (was 4)');
|
||||
$four->getNode()->moveAsFirstChildOf($one_one);
|
||||
|
||||
$root->refresh();
|
||||
$one_two_one->refresh();
|
||||
$one_two_one->set('name', 'last (was 1.2.1)');
|
||||
$one_two_one->getNode()->moveAsLastChildOf($root);
|
||||
|
||||
output_message('transformed tree');
|
||||
output_tree($root);
|
||||
|
||||
$one_one->refresh();
|
||||
$one_one->deleteNode();
|
||||
|
||||
output_message('delete 1.1');
|
||||
output_tree($root);
|
||||
|
||||
// now test fetching root
|
||||
$tree_root = $manager->getTable('Menu')->getTree()->findRoot();
|
||||
output_message('testing fetch root and outputting tree from the root node');
|
||||
output_tree($tree_root);
|
||||
|
||||
// now test fetching the tree
|
||||
output_message('testing fetching entire tree using tree::fetchTree()');
|
||||
$tree = $manager->getTable('Menu')->getTree()->fetchTree();
|
||||
while($node = $tree->next())
|
||||
{
|
||||
output_node($node);
|
||||
}
|
||||
|
||||
// now test fetching the tree
|
||||
output_message('testing fetching entire tree using tree::fetchTree(), excluding root node');
|
||||
$tree = $manager->getTable('Menu')->getTree()->fetchTree(array('include_record' => false));
|
||||
while($node = $tree->next())
|
||||
{
|
||||
output_node($node);
|
||||
}
|
||||
|
||||
// now test fetching the branch
|
||||
output_message('testing fetching branch for 1, using tree::fetchBranch()');
|
||||
$one->refresh();
|
||||
$branch = $manager->getTable('Menu')->getTree()->fetchBranch($one->get('id'));
|
||||
while($node = $branch->next())
|
||||
{
|
||||
output_node($node);
|
||||
}
|
||||
|
||||
// now test fetching the tree
|
||||
output_message('testing fetching branch for 1, using tree::fetchBranch() excluding node 1');
|
||||
$tree = $manager->getTable('Menu')->getTree()->fetchBranch($one->get('id'), array('include_record' => false));
|
||||
while($node = $tree->next())
|
||||
{
|
||||
output_node($node);
|
||||
}
|
||||
|
||||
// now perform some tests
|
||||
output_message('descendants for 1');
|
||||
$descendants = $one->getNode()->getDescendants();
|
||||
while($descendant = $descendants->next())
|
||||
{
|
||||
output_node($descendant);
|
||||
}
|
||||
|
||||
// move one and children under two
|
||||
$two->refresh();
|
||||
$one->getNode()->moveAsFirstChildOf($two);
|
||||
|
||||
output_message('moved one as first child of 2');
|
||||
output_tree($root);
|
||||
|
||||
output_message('descendants for 2');
|
||||
$two->refresh();
|
||||
$descendants = $two->getNode()->getDescendants();
|
||||
while($descendant = $descendants->next())
|
||||
{
|
||||
output_node($descendant);
|
||||
}
|
||||
|
||||
output_message('number descendants for 2');
|
||||
echo $two->getNode()->getNumberDescendants() .'</br>';
|
||||
|
||||
output_message('children for 2 (notice excludes children of children, known as descendants)');
|
||||
$children = $two->getNode()->getChildren();
|
||||
while($child = $children->next())
|
||||
{
|
||||
output_node($child);
|
||||
}
|
||||
|
||||
output_message('number children for 2');
|
||||
echo $two->getNode()->getNumberChildren() .'</br>';
|
||||
|
||||
output_message('path to 1');
|
||||
$path = $one->getNode()->getPath(' > ');
|
||||
echo $path .'<br />';
|
||||
|
||||
output_message('path to 1 (including 1)');
|
||||
$path = $one->getNode()->getPath(' > ', true);
|
||||
echo $path .'<br />';
|
||||
|
||||
output_message('1 has parent');
|
||||
$hasParent = $one->getNode()->hasParent();
|
||||
$msg = $hasParent ? 'true' : 'false';
|
||||
echo $msg . '</br/>';
|
||||
|
||||
output_message('parent to 1');
|
||||
$parent = $one->getNode()->getParent();
|
||||
if($parent->exists())
|
||||
{
|
||||
echo $parent->get('name') .'<br />';
|
||||
}
|
||||
|
||||
output_message('root isRoot?');
|
||||
$isRoot = $root->getNode()->isRoot();
|
||||
$msg = $isRoot ? 'true' : 'false';
|
||||
echo $msg . '</br/>';
|
||||
|
||||
output_message('one isRoot?');
|
||||
$isRoot = $one->getNode()->isRoot();
|
||||
$msg = $isRoot ? 'true' : 'false';
|
||||
echo $msg . '</br/>';
|
||||
|
||||
output_message('root hasParent');
|
||||
$hasParent = $root->getNode()->hasParent();
|
||||
$msg = $hasParent ? 'true' : 'false';
|
||||
echo $msg . '</br/>';
|
||||
|
||||
output_message('root getParent');
|
||||
$parent = $root->getNode()->getParent();
|
||||
if($parent->exists())
|
||||
{
|
||||
echo $parent->get('name') .'<br />';
|
||||
}
|
||||
|
||||
output_message('get first child of root');
|
||||
$record = $root->getNode()->getFirstChild();
|
||||
if($record->exists())
|
||||
{
|
||||
echo $record->get('name') .'<br />';
|
||||
}
|
||||
|
||||
output_message('get last child of root');
|
||||
$record = $root->getNode()->getLastChild();
|
||||
if($record->exists())
|
||||
{
|
||||
echo $record->get('name') .'<br />';
|
||||
}
|
||||
|
||||
$one_two->refresh();
|
||||
|
||||
output_message('get prev sibling of 1.2');
|
||||
$record = $one_two->getNode()->getPrevSibling();
|
||||
if($record->exists())
|
||||
{
|
||||
echo $record->get('name') .'<br />';
|
||||
}
|
||||
|
||||
output_message('get next sibling of 1.2');
|
||||
$record = $one_two->getNode()->getNextSibling();
|
||||
if($record->exists())
|
||||
{
|
||||
echo $record->get('name') .'<br />';
|
||||
}
|
||||
|
||||
output_message('siblings of 1.2');
|
||||
$siblings = $one_two->getNode()->getSiblings();
|
||||
foreach($siblings as $sibling)
|
||||
{
|
||||
if($sibling->exists())
|
||||
echo $sibling->get('name') .'<br />';
|
||||
}
|
||||
|
||||
output_message('siblings of 1.2 (including 1.2)');
|
||||
$siblings = $one_two->getNode()->getSiblings(true);
|
||||
foreach($siblings as $sibling)
|
||||
{
|
||||
if($sibling->exists())
|
||||
echo $sibling->get('name') .'<br />';
|
||||
}
|
||||
|
||||
$new = new Menu();
|
||||
$new->set('name', 'parent of 1.2');
|
||||
$new->getNode()->insertAsParentOf($one_two);
|
||||
|
||||
output_message('added a parent to 1.2');
|
||||
output_tree($root);
|
||||
|
||||
try {
|
||||
$dummy = new Menu();
|
||||
$dummy->set('name', 'dummy');
|
||||
$dummy->save();
|
||||
}
|
||||
catch (Doctrine_Exception $e)
|
||||
{
|
||||
output_message('You cannot save a node unless it is in the tree');
|
||||
}
|
||||
|
||||
try {
|
||||
$fake = new Menu();
|
||||
$fake->set('name', 'dummy');
|
||||
$fake->set('lft', 200);
|
||||
$fake->set('rgt', 1);
|
||||
$fake->save();
|
||||
}
|
||||
catch (Doctrine_Exception $e)
|
||||
{
|
||||
output_message('You cannot save a node with bad lft and rgt values');
|
||||
}
|
||||
|
||||
// check last remaining tests
|
||||
output_message('New parent is descendant of 1');
|
||||
$one->refresh();
|
||||
$res = $new->getNode()->isDescendantOf($one);
|
||||
$msg = $res ? 'true' : 'false';
|
||||
echo $msg . '</br/>';
|
||||
|
||||
output_message('New parent is descendant of 2');
|
||||
$two->refresh();
|
||||
$res = $new->getNode()->isDescendantOf($two);
|
||||
$msg = $res ? 'true' : 'false';
|
||||
echo $msg . '</br/>';
|
||||
|
||||
output_message('New parent is descendant of 1.2');
|
||||
$one_two->refresh();
|
||||
$res = $new->getNode()->isDescendantOf($one_two);
|
||||
$msg = $res ? 'true' : 'false';
|
||||
echo $msg . '</br/>';
|
||||
|
||||
output_message('New parent is descendant of or equal to 1');
|
||||
$one->refresh();
|
||||
$res = $new->getNode()->isDescendantOfOrEqualTo($one);
|
||||
$msg = $res ? 'true' : 'false';
|
||||
echo $msg . '</br/>';
|
||||
|
||||
output_message('New parent is descendant of or equal to 1.2');
|
||||
$one_two->refresh();
|
||||
$res = $new->getNode()->isDescendantOfOrEqualTo($one_two);
|
||||
$msg = $res ? 'true' : 'false';
|
||||
echo $msg . '</br/>';
|
||||
|
||||
output_message('New parent is descendant of or equal to 1.3');
|
||||
$five->refresh();
|
||||
$res = $new->getNode()->isDescendantOfOrEqualTo($new);
|
||||
$msg = $res ? 'true' : 'false';
|
||||
echo $msg . '</br/>';
|
||||
|
||||
function output_tree($root)
|
||||
{
|
||||
// display tree
|
||||
// first we must refresh the node as the tree has been transformed
|
||||
$root->refresh();
|
||||
|
||||
// next we must get the iterator to traverse the tree from the root node
|
||||
$traverse = $root->getNode()->traverse();
|
||||
|
||||
output_node($root);
|
||||
// now we traverse the tree and output the menu items
|
||||
while($item = $traverse->next())
|
||||
{
|
||||
output_node($item);
|
||||
}
|
||||
|
||||
unset($traverse);
|
||||
}
|
||||
|
||||
function output_node($record)
|
||||
{
|
||||
echo str_repeat('-', $record->getNode()->getLevel()) . $record->get('name') . ' (has children:'.$record->getNode()->hasChildren().') '. ' (is leaf:'.$record->getNode()->isLeaf().') '.'<br/>';
|
||||
}
|
||||
|
||||
function output_message($msg)
|
||||
{
|
||||
echo "<br /><strong><em>$msg</em></strong>".'<br />';
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -1,53 +0,0 @@
|
||||
<?php
|
||||
/*
|
||||
* $Id$
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* This software consists of voluntary contributions made by many individuals
|
||||
* and is licensed under the LGPL. For more information, see
|
||||
* <http://www.phpdoctrine.com>.
|
||||
*/
|
||||
/**
|
||||
* Doctrine_Tree_NestedSet
|
||||
*
|
||||
* the purpose of Doctrine_Tree_NestedSet is to provide NestedSet tree access
|
||||
* strategy for all records extending it
|
||||
*
|
||||
* @package Doctrine ORM
|
||||
* @url www.phpdoctrine.com
|
||||
* @license LGPL
|
||||
*/
|
||||
class Doctrine_Tree_NestedSet extends Doctrine_Record {
|
||||
|
||||
public function getLeafNodes() {
|
||||
$query = "SELECT ".implode(", ",$this->table->getColumnNames()).
|
||||
" FROM ".$this->table->getTableName().
|
||||
" WHERE rgt = lft + 1";
|
||||
}
|
||||
|
||||
public function getPath() { }
|
||||
|
||||
public function getDepth() {
|
||||
$query = "SELECT (COUNT(parent.name) - 1) AS depth
|
||||
FROM ".$this->table->getTableName()." AS node,".
|
||||
$this->table->getTableName()." AS parent
|
||||
WHERE node.lft BETWEEN parent.lft AND parent.rgt
|
||||
GROUP BY node.name";
|
||||
}
|
||||
|
||||
public function removeNode() { }
|
||||
|
||||
public function addNode() { }
|
||||
}
|
||||
|
149
draft/Node.php
Normal file
149
draft/Node.php
Normal file
@ -0,0 +1,149 @@
|
||||
<?php
|
||||
/*
|
||||
* $Id$
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* This software consists of voluntary contributions made by many individuals
|
||||
* and is licensed under the LGPL. For more information, see
|
||||
* <http://www.phpdoctrine.com>.
|
||||
*/
|
||||
/**
|
||||
* Doctrine_Node
|
||||
*
|
||||
* @package Doctrine
|
||||
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
||||
* @category Object Relational Mapping
|
||||
* @link www.phpdoctrine.com
|
||||
* @since 1.0
|
||||
* @version $Revision$
|
||||
* @author Joe Simms <joe.simms@websites4.com>
|
||||
*/
|
||||
class Doctrine_Node implements IteratorAggregate
|
||||
{
|
||||
/**
|
||||
* @param object $record reference to associated Doctrine_Record instance
|
||||
*/
|
||||
protected $record;
|
||||
|
||||
/**
|
||||
* @param array $options
|
||||
*/
|
||||
protected $options;
|
||||
|
||||
/**
|
||||
* @param string $iteratorType (Pre | Post | Level)
|
||||
*/
|
||||
protected $iteratorType;
|
||||
|
||||
/**
|
||||
* @param array $iteratorOptions
|
||||
*/
|
||||
protected $iteratorOptions;
|
||||
|
||||
/**
|
||||
* contructor, creates node with reference to record and any options
|
||||
*
|
||||
* @param object $record instance of Doctrine_Record
|
||||
* @param array $options options
|
||||
*/
|
||||
public function __construct(&$record, $options)
|
||||
{
|
||||
$this->record = $record;
|
||||
$this->options = $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* factory method to return node instance based upon chosen implementation
|
||||
*
|
||||
* @param object $record instance of Doctrine_Record
|
||||
* @param string $impName implementation (NestedSet, AdjacencyList, MaterializedPath)
|
||||
* @param array $options options
|
||||
* @return object $options instance of Doctrine_Node
|
||||
*/
|
||||
public static function factory(&$record, $implName, $options = array()) {
|
||||
|
||||
$class = 'Doctrine_Node_'.$implName;
|
||||
|
||||
if(!class_exists($class))
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* getter for record attribute
|
||||
*
|
||||
* @return object instance of Doctrine_Record
|
||||
*/
|
||||
public function getRecord() {
|
||||
return $this->record;
|
||||
}
|
||||
|
||||
/**
|
||||
* convenience function for getIterator
|
||||
*
|
||||
* @param string $type type of iterator (Pre | Post | Level)
|
||||
* @param array $options options
|
||||
*/
|
||||
public function traverse($type = 'Pre', $options = array()) {
|
||||
return $this->getIterator($type, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* get iterator
|
||||
*
|
||||
* @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);
|
||||
}
|
||||
|
||||
/**
|
||||
* sets node's iterator type
|
||||
*
|
||||
* @param int
|
||||
*/
|
||||
public function setIteratorType($type) {
|
||||
$this->iteratorType = $type;
|
||||
}
|
||||
|
||||
/**
|
||||
* sets node's iterator options
|
||||
*
|
||||
* @param int
|
||||
*/
|
||||
public function setIteratorOptions($options) {
|
||||
$this->iteratorOptions = $options;
|
||||
}
|
||||
} // END class
|
@ -19,26 +19,15 @@
|
||||
* <http://www.phpdoctrine.com>.
|
||||
*/
|
||||
/**
|
||||
* Doctrine_Tree_PathModel
|
||||
* Doctrine_Node_AdjacencyList
|
||||
*
|
||||
* the purpose of Doctrine_Tree_PathModel is to provide PathModel tree access
|
||||
* strategy for all records extending it
|
||||
*
|
||||
* @package Doctrine ORM
|
||||
* @url www.phpdoctrine.com
|
||||
* @license LGPL
|
||||
* @package Doctrine
|
||||
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
||||
* @category Object Relational Mapping
|
||||
* @link www.phpdoctrine.com
|
||||
* @since 1.0
|
||||
* @version $Revision$
|
||||
* @author Joe Simms <joe.simms@websites4.com>
|
||||
*/
|
||||
class Doctrine_Tree_PathModel extends Doctrine_Record {
|
||||
|
||||
public function getLeafNodes() { }
|
||||
|
||||
public function getPath() { }
|
||||
|
||||
public function getDepth() { }
|
||||
|
||||
public function removeNode() { }
|
||||
|
||||
public function addNode() { }
|
||||
|
||||
}
|
||||
class Doctrine_Node_AdjacencyList extends Doctrine_Node implements Doctrine_Node_Interface { }
|
||||
|
32
draft/Node/AdjacencyList/LevelOrderIterator.php
Normal file
32
draft/Node/AdjacencyList/LevelOrderIterator.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
/*
|
||||
* $Id$
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* This software consists of voluntary contributions made by many individuals
|
||||
* and is licensed under the LGPL. For more information, see
|
||||
* <http://www.phpdoctrine.com>.
|
||||
*/
|
||||
/**
|
||||
* Doctrine_Node_AdjacencyList_LevelOrderIterator
|
||||
*
|
||||
* @package Doctrine
|
||||
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
||||
* @category Object Relational Mapping
|
||||
* @link www.phpdoctrine.com
|
||||
* @since 1.0
|
||||
* @version $Revision$
|
||||
* @author Joe Simms <joe.simms@websites4.com>
|
||||
*/
|
||||
class Doctrine_Node_AdjacencyList_LevelOrderIterator implements Iterator {}
|
32
draft/Node/AdjacencyList/PostOrderIterator.php
Normal file
32
draft/Node/AdjacencyList/PostOrderIterator.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
/*
|
||||
* $Id$
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* This software consists of voluntary contributions made by many individuals
|
||||
* and is licensed under the LGPL. For more information, see
|
||||
* <http://www.phpdoctrine.com>.
|
||||
*/
|
||||
/**
|
||||
* Doctrine_Node_AdjacencyList_PostOrderIterator
|
||||
*
|
||||
* @package Doctrine
|
||||
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
||||
* @category Object Relational Mapping
|
||||
* @link www.phpdoctrine.com
|
||||
* @since 1.0
|
||||
* @version $Revision$
|
||||
* @author Joe Simms <joe.simms@websites4.com>
|
||||
*/
|
||||
class Doctrine_Node_AdjacencyList_PostOrderIterator implements Iterator {}
|
32
draft/Node/AdjacencyList/PreOrderIterator.php
Normal file
32
draft/Node/AdjacencyList/PreOrderIterator.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
/*
|
||||
* $Id$
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* This software consists of voluntary contributions made by many individuals
|
||||
* and is licensed under the LGPL. For more information, see
|
||||
* <http://www.phpdoctrine.com>.
|
||||
*/
|
||||
/**
|
||||
* Doctrine_Node_AdjacencyList_PreOrderIterator
|
||||
*
|
||||
* @package Doctrine
|
||||
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
||||
* @category Object Relational Mapping
|
||||
* @link www.phpdoctrine.com
|
||||
* @since 1.0
|
||||
* @version $Revision$
|
||||
* @author Joe Simms <joe.simms@websites4.com>
|
||||
*/
|
||||
class Doctrine_Node_AdjacencyList_PreOrderIterator implements Iterator {}
|
32
draft/Node/Exception.php
Normal file
32
draft/Node/Exception.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
/*
|
||||
* $Id$
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* This software consists of voluntary contributions made by many individuals
|
||||
* and is licensed under the LGPL. For more information, see
|
||||
* <http://www.phpdoctrine.com>.
|
||||
*/
|
||||
/**
|
||||
* Doctrine_Node_Exception
|
||||
*
|
||||
* @package Doctrine
|
||||
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
||||
* @category Object Relational Mapping
|
||||
* @link www.phpdoctrine.com
|
||||
* @since 1.0
|
||||
* @version $Revision$
|
||||
* @author Joe Simms <joe.simms@websites4.com>
|
||||
*/
|
||||
class Doctrine_Node_Exception extends Doctrine_Exception { }
|
267
draft/Node/Interface.php
Normal file
267
draft/Node/Interface.php
Normal file
@ -0,0 +1,267 @@
|
||||
<?php
|
||||
/*
|
||||
* $Id$
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* This software consists of voluntary contributions made by many individuals
|
||||
* and is licensed under the LGPL. For more information, see
|
||||
* <http://www.phpdoctrine.com>.
|
||||
*/
|
||||
/**
|
||||
* Doctrine_Node_Interface
|
||||
*
|
||||
* @package Doctrine
|
||||
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
||||
* @category Object Relational Mapping
|
||||
* @link www.phpdoctrine.com
|
||||
* @since 1.0
|
||||
* @version $Revision$
|
||||
* @author Joe Simms <joe.simms@websites4.com>
|
||||
*/
|
||||
interface Doctrine_Node_Interface {
|
||||
|
||||
/**
|
||||
* test if node has previous sibling
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasPrevSibling();
|
||||
|
||||
/**
|
||||
* test if node has next sibling
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasNextSibling();
|
||||
|
||||
/**
|
||||
* test if node has children
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasChildren();
|
||||
|
||||
/**
|
||||
* test if node has parent
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasParent();
|
||||
|
||||
/**
|
||||
* gets record of prev sibling or empty record
|
||||
*
|
||||
* @return object Doctrine_Record
|
||||
*/
|
||||
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
|
||||
*
|
||||
* @return object Doctrine_Record
|
||||
*/
|
||||
public function getFirstChild();
|
||||
|
||||
/**
|
||||
* gets record of last child or empty record
|
||||
*
|
||||
* @return object Doctrine_Record
|
||||
*/
|
||||
public function getLastChild();
|
||||
|
||||
/**
|
||||
* gets children for node (direct descendants only)
|
||||
*
|
||||
* @return array array of sibling Doctrine_Record objects
|
||||
*/
|
||||
public function getChildren();
|
||||
|
||||
/**
|
||||
* gets descendants for node (direct descendants only)
|
||||
*
|
||||
* @return iterator iterator to traverse descendants from node
|
||||
*/
|
||||
public function getDescendants();
|
||||
|
||||
/**
|
||||
* gets record of parent or empty record
|
||||
*
|
||||
* @return object Doctrine_Record
|
||||
*/
|
||||
public function getParent();
|
||||
|
||||
/**
|
||||
* gets ancestors for node
|
||||
*
|
||||
* @return object Doctrine_Collection
|
||||
*/
|
||||
public function getAncestors();
|
||||
|
||||
/**
|
||||
* gets path to node from root, uses record::toString() method to get node names
|
||||
*
|
||||
* @param string $seperator path seperator
|
||||
* @param bool $includeNode whether or not to include node at end of path
|
||||
* @return string string representation of path
|
||||
*/
|
||||
public function getPath($seperator = ' > ', $includeNode = false);
|
||||
|
||||
/**
|
||||
* gets level (depth) of node in the tree
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getLevel();
|
||||
|
||||
/**
|
||||
* gets number of children (direct descendants)
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getNumberChildren();
|
||||
|
||||
/**
|
||||
* gets number of descendants (children and their children)
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getNumberDescendants();
|
||||
|
||||
/**
|
||||
* inserts node as parent of dest record
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function insertAsParentOf(Doctrine_Record $dest);
|
||||
|
||||
/**
|
||||
* inserts node as previous sibling of dest record
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function insertAsPrevSiblingOf(Doctrine_Record $dest);
|
||||
|
||||
/**
|
||||
* inserts node as next sibling of dest record
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function insertAsNextSiblingOf(Doctrine_Record $dest);
|
||||
|
||||
/**
|
||||
* inserts node as first child of dest record
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function insertAsFirstChildOf(Doctrine_Record $dest);
|
||||
|
||||
/**
|
||||
* inserts node as first child of dest record
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function insertAsLastChildOf(Doctrine_Record $dest);
|
||||
|
||||
/**
|
||||
* moves node as prev sibling of dest record
|
||||
*
|
||||
*/
|
||||
public function moveAsPrevSiblingOf(Doctrine_Record $dest);
|
||||
|
||||
/**
|
||||
* moves node as next sibling of dest record
|
||||
*
|
||||
*/
|
||||
public function moveAsNextSiblingOf(Doctrine_Record $dest);
|
||||
|
||||
/**
|
||||
* moves node as first child of dest record
|
||||
*
|
||||
*/
|
||||
public function moveAsFirstChildOf(Doctrine_Record $dest);
|
||||
|
||||
/**
|
||||
* moves node as last child of dest record
|
||||
*
|
||||
*/
|
||||
public function moveAsLastChildOf(Doctrine_Record $dest);
|
||||
|
||||
/**
|
||||
* adds node as last child of record
|
||||
*
|
||||
*/
|
||||
public function addChild(Doctrine_Record $record);
|
||||
|
||||
/**
|
||||
* determines if node is leaf
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isLeaf();
|
||||
|
||||
/**
|
||||
* determines if node is root
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isRoot();
|
||||
|
||||
/**
|
||||
* determines if node is equal to subject node
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isEqualTo(Doctrine_Record $subj);
|
||||
|
||||
/**
|
||||
* determines if node is child of subject node
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isDescendantOf(Doctrine_Record $subj);
|
||||
|
||||
/**
|
||||
* determines if node is child of or sibling to subject node
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
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();
|
||||
}
|
33
draft/Node/MaterializedPath.php
Normal file
33
draft/Node/MaterializedPath.php
Normal file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
/*
|
||||
* $Id$
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* This software consists of voluntary contributions made by many individuals
|
||||
* and is licensed under the LGPL. For more information, see
|
||||
* <http://www.phpdoctrine.com>.
|
||||
*/
|
||||
/**
|
||||
* Doctrine_Node_MaterializedPath
|
||||
*
|
||||
* @package Doctrine
|
||||
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
||||
* @category Object Relational Mapping
|
||||
* @link www.phpdoctrine.com
|
||||
* @since 1.0
|
||||
* @version $Revision$
|
||||
* @author Joe Simms <joe.simms@websites4.com>
|
||||
*/
|
||||
class Doctrine_Node_MaterializedPath extends Doctrine_Node implements Doctrine_Node_Interface { }
|
||||
|
61
draft/Node/MaterializedPath/LevelOrderIterator.php
Normal file
61
draft/Node/MaterializedPath/LevelOrderIterator.php
Normal file
@ -0,0 +1,61 @@
|
||||
<?php
|
||||
/*
|
||||
* $Id$
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* This software consists of voluntary contributions made by many individuals
|
||||
* and is licensed under the LGPL. For more information, see
|
||||
* <http://www.phpdoctrine.com>.
|
||||
*/
|
||||
/**
|
||||
* Doctrine_Node_MaterializedPath_LevelOrderIterator
|
||||
*
|
||||
* @package Doctrine
|
||||
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
||||
* @category Object Relational Mapping
|
||||
* @link www.phpdoctrine.com
|
||||
* @since 1.0
|
||||
* @version $Revision$
|
||||
* @author Joe Simms <joe.simms@websites4.com>
|
||||
*/
|
||||
class Doctrine_Node_MaterializedPath_LevelOrderIterator implements Iterator
|
||||
{
|
||||
private $topNode = null;
|
||||
|
||||
private $curNode = null;
|
||||
|
||||
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');
|
||||
}
|
||||
|
||||
public function next() {
|
||||
throw new Doctrine_Exception('Not yet implemented');
|
||||
}
|
||||
}
|
61
draft/Node/MaterializedPath/PostOrderIterator.php
Normal file
61
draft/Node/MaterializedPath/PostOrderIterator.php
Normal file
@ -0,0 +1,61 @@
|
||||
<?php
|
||||
/*
|
||||
* $Id$
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* This software consists of voluntary contributions made by many individuals
|
||||
* and is licensed under the LGPL. For more information, see
|
||||
* <http://www.phpdoctrine.com>.
|
||||
*/
|
||||
/**
|
||||
* Doctrine_Node_MaterializedPath_PostOrderIterator
|
||||
*
|
||||
* @package Doctrine
|
||||
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
||||
* @category Object Relational Mapping
|
||||
* @link www.phpdoctrine.com
|
||||
* @since 1.0
|
||||
* @version $Revision$
|
||||
* @author Joe Simms <joe.simms@websites4.com>
|
||||
*/
|
||||
class Doctrine_Node_MaterializedPath_PostOrderIterator implements Iterator
|
||||
{
|
||||
private $topNode = null;
|
||||
|
||||
private $curNode = null;
|
||||
|
||||
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');
|
||||
}
|
||||
|
||||
public function next() {
|
||||
throw new Doctrine_Exception('Not yet implemented');
|
||||
}
|
||||
}
|
61
draft/Node/MaterializedPath/PreOrderIterator.php
Normal file
61
draft/Node/MaterializedPath/PreOrderIterator.php
Normal file
@ -0,0 +1,61 @@
|
||||
<?php
|
||||
/*
|
||||
* $Id$
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* This software consists of voluntary contributions made by many individuals
|
||||
* and is licensed under the LGPL. For more information, see
|
||||
* <http://www.phpdoctrine.com>.
|
||||
*/
|
||||
/**
|
||||
* Doctrine_Node_MaterializedPath_PreOrderIterator
|
||||
*
|
||||
* @package Doctrine
|
||||
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
||||
* @category Object Relational Mapping
|
||||
* @link www.phpdoctrine.com
|
||||
* @since 1.0
|
||||
* @version $Revision$
|
||||
* @author Joe Simms <joe.simms@websites4.com>
|
||||
*/
|
||||
class Doctrine_Node_MaterializedPath_PreOrderIterator implements Iterator
|
||||
{
|
||||
private $topNode = null;
|
||||
|
||||
private $curNode = null;
|
||||
|
||||
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');
|
||||
}
|
||||
|
||||
public function next() {
|
||||
throw new Doctrine_Exception('Not yet implemented');
|
||||
}
|
||||
}
|
673
draft/Node/NestedSet.php
Normal file
673
draft/Node/NestedSet.php
Normal file
@ -0,0 +1,673 @@
|
||||
<?php
|
||||
/*
|
||||
* $Id$
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* This software consists of voluntary contributions made by many individuals
|
||||
* and is licensed under the LGPL. For more information, see
|
||||
* <http://www.phpdoctrine.com>.
|
||||
*/
|
||||
/**
|
||||
* Doctrine_Node_NestedSet
|
||||
*
|
||||
* @package Doctrine
|
||||
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
||||
* @category Object Relational Mapping
|
||||
* @link www.phpdoctrine.com
|
||||
* @since 1.0
|
||||
* @version $Revision$
|
||||
* @author Joe Simms <joe.simms@websites4.com>
|
||||
*/
|
||||
class Doctrine_Node_NestedSet extends Doctrine_Node implements Doctrine_Node_Interface
|
||||
{
|
||||
/**
|
||||
* test if node has previous sibling
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasPrevSibling()
|
||||
{
|
||||
return $this->isValidNode($this->getPrevSibling());
|
||||
}
|
||||
|
||||
/**
|
||||
* test if node has next sibling
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasNextSibling()
|
||||
{
|
||||
return $this->isValidNode($this->getNextSibling());
|
||||
}
|
||||
|
||||
/**
|
||||
* test if node has children
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasChildren()
|
||||
{
|
||||
return (($this->getRightValue() - $this->getLeftValue() ) >1 );
|
||||
}
|
||||
|
||||
/**
|
||||
* test if node has parent
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasParent()
|
||||
{
|
||||
return !$this->isRoot();
|
||||
}
|
||||
|
||||
/**
|
||||
* gets record of prev sibling or empty record
|
||||
*
|
||||
* @return object Doctrine_Record
|
||||
*/
|
||||
public function getPrevSibling()
|
||||
{
|
||||
$q = $this->record->getTable()->createQuery();
|
||||
$result = $q->where('rgt = ?', $this->getLeftValue() - 1)->execute()->getFirst();
|
||||
|
||||
if(!$result)
|
||||
$result = $this->record->getTable()->create();
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* gets record of next sibling or empty record
|
||||
*
|
||||
* @return object Doctrine_Record
|
||||
*/
|
||||
public function getNextSibling()
|
||||
{
|
||||
$q = $this->record->getTable()->createQuery();
|
||||
$result = $q->where('lft = ?', $this->getRightValue() + 1)->execute()->getFirst();
|
||||
|
||||
if(!$result)
|
||||
$result = $this->record->getTable()->create();
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* gets siblings for node
|
||||
*
|
||||
* @return array array of sibling Doctrine_Record objects
|
||||
*/
|
||||
public function getSiblings($includeNode = false)
|
||||
{
|
||||
$parent = $this->getParent();
|
||||
$siblings = array();
|
||||
if($parent->exists())
|
||||
{
|
||||
foreach($parent->getNode()->getChildren() as $child)
|
||||
{
|
||||
if($this->isEqualTo($child) && !$includeNode)
|
||||
continue;
|
||||
|
||||
$siblings[] = $child;
|
||||
}
|
||||
}
|
||||
|
||||
return $siblings;
|
||||
}
|
||||
|
||||
/**
|
||||
* gets record of first child or empty record
|
||||
*
|
||||
* @return object Doctrine_Record
|
||||
*/
|
||||
public function getFirstChild()
|
||||
{
|
||||
$q = $this->record->getTable()->createQuery();
|
||||
$result = $q->where('lft = ?', $this->getLeftValue() + 1)->execute()->getFirst();
|
||||
|
||||
if(!$result)
|
||||
$result = $this->record->getTable()->create();
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* gets record of last child or empty record
|
||||
*
|
||||
* @return object Doctrine_Record
|
||||
*/
|
||||
public function getLastChild()
|
||||
{
|
||||
$q = $this->record->getTable()->createQuery();
|
||||
$result = $q->where('rgt = ?', $this->getRightValue() - 1)->execute()->getFirst();
|
||||
|
||||
if(!$result)
|
||||
$result = $this->record->getTable()->create();
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* gets children for node (direct descendants only)
|
||||
*
|
||||
* @return array array of sibling Doctrine_Record objects
|
||||
*/
|
||||
public function getChildren()
|
||||
{
|
||||
return $this->getIterator('Pre', array('depth' => 1));
|
||||
}
|
||||
|
||||
/**
|
||||
* gets descendants for node (direct descendants only)
|
||||
*
|
||||
* @return iterator iterator to traverse descendants from node
|
||||
*/
|
||||
public function getDescendants()
|
||||
{
|
||||
return $this->getIterator();
|
||||
}
|
||||
|
||||
/**
|
||||
* gets record of parent or empty record
|
||||
*
|
||||
* @return object Doctrine_Record
|
||||
*/
|
||||
public function getParent()
|
||||
{
|
||||
$q = $this->record->getTable()->createQuery();
|
||||
|
||||
$parent = $q->where('lft < ? AND rgt > ?', array($this->getLeftValue(), $this->getRightValue()))
|
||||
->orderBy('rgt asc')
|
||||
->execute()
|
||||
->getFirst();
|
||||
|
||||
if(!$parent)
|
||||
$parent = $this->record->getTable()->create();
|
||||
|
||||
return $parent;
|
||||
}
|
||||
|
||||
/**
|
||||
* gets ancestors for node
|
||||
*
|
||||
* @return object Doctrine_Collection
|
||||
*/
|
||||
public function getAncestors()
|
||||
{
|
||||
$q = $this->record->getTable()->createQuery();
|
||||
|
||||
$ancestors = $q->where('lft < ? AND rgt > ?', array($this->getLeftValue(), $this->getRightValue()))
|
||||
->orderBy('lft asc')
|
||||
->execute();
|
||||
|
||||
return $ancestors;
|
||||
}
|
||||
|
||||
/**
|
||||
* gets path to node from root, uses record::toString() method to get node names
|
||||
*
|
||||
* @param string $seperator path seperator
|
||||
* @param bool $includeNode whether or not to include node at end of path
|
||||
* @return string string representation of path
|
||||
*/
|
||||
public function getPath($seperator = ' > ', $includeRecord = false)
|
||||
{
|
||||
$path = array();
|
||||
$ancestors = $this->getAncestors();
|
||||
foreach($ancestors as $ancestor)
|
||||
{
|
||||
$path[] = $ancestor->toString();
|
||||
}
|
||||
if($includeRecord)
|
||||
$path[] = $this->getRecord()->toString();
|
||||
|
||||
return implode($seperator, $path);
|
||||
}
|
||||
|
||||
/**
|
||||
* gets number of children (direct descendants)
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getNumberChildren()
|
||||
{
|
||||
$count = 0;
|
||||
$children = $this->getChildren();
|
||||
|
||||
while($children->next())
|
||||
{
|
||||
$count++;
|
||||
}
|
||||
return $count;
|
||||
}
|
||||
|
||||
/**
|
||||
* gets number of descendants (children and their children)
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getNumberDescendants()
|
||||
{
|
||||
return ($this->getRightValue() - $this->getLeftValue() - 1) / 2;
|
||||
}
|
||||
|
||||
/**
|
||||
* inserts node as parent of dest record
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function insertAsParentOf(Doctrine_Record $dest)
|
||||
{
|
||||
// cannot insert a node that has already has a place within the tree
|
||||
if($this->isValidNode())
|
||||
return false;
|
||||
|
||||
// cannot insert as parent of root
|
||||
if($dest->getNode()->isRoot())
|
||||
return false;
|
||||
|
||||
$this->shiftRLValues($dest->getNode()->getLeftValue(), 1);
|
||||
$this->shiftRLValues($dest->getNode()->getRightValue() + 2, 1);
|
||||
|
||||
$newLeft = $dest->getNode()->getLeftValue();
|
||||
$newRight = $dest->getNode()->getRightValue() + 2;
|
||||
$this->insertNode($newLeft, $newRight);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* inserts node as previous sibling of dest record
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function insertAsPrevSiblingOf(Doctrine_Record $dest)
|
||||
{
|
||||
// cannot insert a node that has already has a place within the tree
|
||||
if($this->isValidNode())
|
||||
return false;
|
||||
|
||||
$newLeft = $dest->getNode()->getLeftValue();
|
||||
$newRight = $dest->getNode()->getLeftValue() + 1;
|
||||
|
||||
$this->shiftRLValues($newLeft, 2);
|
||||
$this->insertNode($newLeft, $newRight);
|
||||
|
||||
// update destination left/right values to prevent a refresh
|
||||
// $dest->getNode()->setLeftValue($dest->getNode()->getLeftValue() + 2);
|
||||
// $dest->getNode()->setRightValue($dest->getNode()->getRightValue() + 2);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* inserts node as next sibling of dest record
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function insertAsNextSiblingOf(Doctrine_Record $dest)
|
||||
{
|
||||
// cannot insert a node that has already has a place within the tree
|
||||
if($this->isValidNode())
|
||||
return false;
|
||||
|
||||
$newLeft = $dest->getNode()->getRightValue() + 1;
|
||||
$newRight = $dest->getNode()->getRightValue() + 2;
|
||||
|
||||
$this->shiftRLValues($newLeft, 2);
|
||||
$this->insertNode($newLeft, $newRight);
|
||||
|
||||
// update destination left/right values to prevent a refresh
|
||||
// no need, node not affected
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* inserts node as first child of dest record
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function insertAsFirstChildOf(Doctrine_Record $dest)
|
||||
{
|
||||
// cannot insert a node that has already has a place within the tree
|
||||
if($this->isValidNode())
|
||||
return false;
|
||||
|
||||
$newLeft = $dest->getNode()->getLeftValue() + 1;
|
||||
$newRight = $dest->getNode()->getLeftValue() + 2;
|
||||
|
||||
$this->shiftRLValues($newLeft, 2);
|
||||
$this->insertNode($newLeft, $newRight);
|
||||
|
||||
// update destination left/right values to prevent a refresh
|
||||
// $dest->getNode()->setRightValue($dest->getNode()->getRightValue() + 2);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* inserts node as last child of dest record
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function insertAsLastChildOf(Doctrine_Record $dest)
|
||||
{
|
||||
// cannot insert a node that has already has a place within the tree
|
||||
if($this->isValidNode())
|
||||
return false;
|
||||
|
||||
$newLeft = $dest->getNode()->getRightValue();
|
||||
$newRight = $dest->getNode()->getRightValue() + 1;
|
||||
|
||||
$this->shiftRLValues($newLeft, 2);
|
||||
$this->insertNode($newLeft, $newRight);
|
||||
|
||||
// update destination left/right values to prevent a refresh
|
||||
// $dest->getNode()->setRightValue($dest->getNode()->getRightValue() + 2);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* moves node as prev sibling of dest record
|
||||
*
|
||||
*/
|
||||
public function moveAsPrevSiblingOf(Doctrine_Record $dest)
|
||||
{
|
||||
$this->updateNode($dest->getNode()->getLeftValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* moves node as next sibling of dest record
|
||||
*
|
||||
*/
|
||||
public function moveAsNextSiblingOf(Doctrine_Record $dest)
|
||||
{
|
||||
$this->updateNode($dest->getNode()->getRightValue() + 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* moves node as first child of dest record
|
||||
*
|
||||
*/
|
||||
public function moveAsFirstChildOf(Doctrine_Record $dest)
|
||||
{
|
||||
$this->updateNode($dest->getNode()->getLeftValue() + 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* moves node as last child of dest record
|
||||
*
|
||||
*/
|
||||
public function moveAsLastChildOf(Doctrine_Record $dest)
|
||||
{
|
||||
$this->updateNode($dest->getNode()->getRightValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* adds node as last child of record
|
||||
*
|
||||
*/
|
||||
public function addChild(Doctrine_Record $record)
|
||||
{
|
||||
$record->getNode()->insertAsLastChildOf($this->getRecord());
|
||||
}
|
||||
|
||||
/**
|
||||
* determines if node is leaf
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isLeaf()
|
||||
{
|
||||
return (($this->getRightValue()-$this->getLeftValue())==1);
|
||||
}
|
||||
|
||||
/**
|
||||
* determines if node is root
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isRoot()
|
||||
{
|
||||
return ($this->getLeftValue()==1);
|
||||
}
|
||||
|
||||
/**
|
||||
* determines if node is equal to subject node
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isEqualTo(Doctrine_Record $subj)
|
||||
{
|
||||
return (($this->getLeftValue()==$subj->getNode()->getLeftValue()) and ($this->getRightValue()==$subj->getNode()->getRightValue()));
|
||||
}
|
||||
|
||||
/**
|
||||
* determines if node is child of subject node
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isDescendantOf(Doctrine_Record $subj)
|
||||
{
|
||||
return (($this->getLeftValue()>$subj->getNode()->getLeftValue()) and ($this->getRightValue()<$subj->getNode()->getRightValue()));
|
||||
}
|
||||
|
||||
/**
|
||||
* determines if node is child of or sibling to subject node
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isDescendantOfOrEqualTo(Doctrine_Record $subj)
|
||||
{
|
||||
return (($this->getLeftValue()>=$subj->getNode()->getLeftValue()) and ($this->getRightValue()<=$subj->getNode()->getRightValue()));
|
||||
}
|
||||
|
||||
/**
|
||||
* determines if node is valid
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isValidNode()
|
||||
{
|
||||
return ($this->getRightValue() > $this->getLeftValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* deletes node and it's descendants
|
||||
*
|
||||
*/
|
||||
public function delete()
|
||||
{
|
||||
// TODO: add the setting whether or not to delete descendants or relocate children
|
||||
|
||||
$q = $this->record->getTable()->createQuery();
|
||||
|
||||
$componentName = $this->record->getTable()->getComponentName();
|
||||
|
||||
$coll = $q->where("$componentName.lft >= ? AND $componentName.rgt <= ?", array($this->getLeftValue(), $this->getRightValue()))->execute();
|
||||
$coll->delete();
|
||||
|
||||
$first = $this->getRightValue() + 1;
|
||||
$delta = $this->getLeftValue() - $this->getRightValue() - 1;
|
||||
$this->shiftRLValues($first, $delta);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* sets node's left and right values and save's it
|
||||
*
|
||||
* @param int $destLeft node left value
|
||||
* @param int $destRight node right value
|
||||
*/
|
||||
private function insertNode($destLeft = 0, $destRight = 0)
|
||||
{
|
||||
$this->setLeftValue($destLeft);
|
||||
$this->setRightValue($destRight);
|
||||
$this->record->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* move node's and its children to location $destLeft and updates rest of tree
|
||||
*
|
||||
* @param int $destLeft destination left value
|
||||
*/
|
||||
private function updateNode($destLeft)
|
||||
{
|
||||
$left = $this->getLeftValue();
|
||||
$right = $this->getRightValue();
|
||||
|
||||
$treeSize = $right - $left + 1;
|
||||
|
||||
$this->shiftRLValues($destLeft, $treeSize);
|
||||
|
||||
if($left >= $destLeft){ // src was shifted too?
|
||||
$left += $treeSize;
|
||||
$right += $treeSize;
|
||||
}
|
||||
|
||||
// now there's enough room next to target to move the subtree
|
||||
$this->shiftRLRange($left, $right, $destLeft - $left);
|
||||
|
||||
// correct values after source
|
||||
$this->shiftRLValues($right + 1, -$treeSize);
|
||||
|
||||
$this->record->save();
|
||||
$this->record->refresh();
|
||||
}
|
||||
|
||||
/**
|
||||
* adds '$delta' to all Left and Right values that are >= '$first'. '$delta' can also be negative.
|
||||
*
|
||||
* @param int $first First node to be shifted
|
||||
* @param int $delta Value to be shifted by, can be negative
|
||||
*/
|
||||
private function shiftRLValues($first, $delta)
|
||||
{
|
||||
$qLeft = $this->record->getTable()->createQuery();
|
||||
$qRight = $this->record->getTable()->createQuery();
|
||||
|
||||
// TODO: Wrap in transaction
|
||||
|
||||
// shift left columns
|
||||
$resultLeft = $qLeft->update($this->record->getTable()->getComponentName())
|
||||
->set('lft', "lft + $delta")
|
||||
->where('lft >= ?', $first)
|
||||
->execute();
|
||||
|
||||
// shift right columns
|
||||
$resultRight = $qRight->update($this->record->getTable()->getComponentName())
|
||||
->set('rgt', "rgt + $delta")
|
||||
->where('rgt >= ?', $first)
|
||||
->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* adds '$delta' to all Left and Right values that are >= '$first' and <= '$last'.
|
||||
* '$delta' can also be negative.
|
||||
*
|
||||
* @param int $first First node to be shifted (L value)
|
||||
* @param int $last Last node to be shifted (L value)
|
||||
* @param int $delta Value to be shifted by, can be negative
|
||||
*/
|
||||
private function shiftRLRange($first, $last, $delta)
|
||||
{
|
||||
$qLeft = $this->record->getTable()->createQuery();
|
||||
$qRight = $this->record->getTable()->createQuery();
|
||||
|
||||
// TODO : Wrap in transaction
|
||||
|
||||
// shift left column values
|
||||
$result = $qLeft->update($this->record->getTable()->getComponentName())
|
||||
->set('lft', "lft + $delta")
|
||||
->where('lft >= ? AND lft <= ?', array($first, $last))
|
||||
->execute();
|
||||
|
||||
// shift right column values
|
||||
$result = $qRight->update($this->record->getTable()->getComponentName())
|
||||
->set('rgt', "rgt + $delta")
|
||||
->where('rgt >= ? AND rgt <= ?', array($first, $last))
|
||||
->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* gets record's left value
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getLeftValue()
|
||||
{
|
||||
return $this->record->get('lft');
|
||||
}
|
||||
|
||||
/**
|
||||
* sets record's left value
|
||||
*
|
||||
* @param int
|
||||
*/
|
||||
public function setLeftValue($lft)
|
||||
{
|
||||
$this->record->set('lft', $lft);
|
||||
}
|
||||
|
||||
/**
|
||||
* gets record's right value
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getRightValue()
|
||||
{
|
||||
return $this->record->get('rgt');
|
||||
}
|
||||
|
||||
/**
|
||||
* sets record's right value
|
||||
*
|
||||
* @param int
|
||||
*/
|
||||
public function setRightValue($rgt)
|
||||
{
|
||||
$this->record->set('rgt', $rgt);
|
||||
}
|
||||
|
||||
/**
|
||||
* gets level (depth) of node in the tree
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getLevel()
|
||||
{
|
||||
if(!isset($this->level))
|
||||
{
|
||||
$q = $this->record->getTable()->createQuery();
|
||||
$coll = $q->where('lft < ? AND rgt > ?', array($this->getLeftValue(), $this->getRightValue()))
|
||||
->execute();
|
||||
$this->level = $coll->count() ? $coll->count() : 0;
|
||||
}
|
||||
|
||||
return $this->level;
|
||||
}
|
||||
|
||||
/**
|
||||
* sets node's level
|
||||
*
|
||||
* @param int
|
||||
*/
|
||||
public function setLevel($level)
|
||||
{
|
||||
$this->level = $level;
|
||||
}
|
||||
}
|
32
draft/Node/NestedSet/LevelOrderIterator.php
Normal file
32
draft/Node/NestedSet/LevelOrderIterator.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
/*
|
||||
* $Id$
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* This software consists of voluntary contributions made by many individuals
|
||||
* and is licensed under the LGPL. For more information, see
|
||||
* <http://www.phpdoctrine.com>.
|
||||
*/
|
||||
/**
|
||||
* Doctrine_Node_NestedSet_LevelOrderIterator
|
||||
*
|
||||
* @package Doctrine
|
||||
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
||||
* @category Object Relational Mapping
|
||||
* @link www.phpdoctrine.com
|
||||
* @since 1.0
|
||||
* @version $Revision$
|
||||
* @author Joe Simms <joe.simms@websites4.com>
|
||||
*/
|
||||
class Doctrine_Node_NestedSet_LevelOrderIterator implements Iterator {}
|
32
draft/Node/NestedSet/PostOrderIterator.php
Normal file
32
draft/Node/NestedSet/PostOrderIterator.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
/*
|
||||
* $Id$
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* This software consists of voluntary contributions made by many individuals
|
||||
* and is licensed under the LGPL. For more information, see
|
||||
* <http://www.phpdoctrine.com>.
|
||||
*/
|
||||
/**
|
||||
* Doctrine_Node_NestedSet_PostOrderIterator
|
||||
*
|
||||
* @package Doctrine
|
||||
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
||||
* @category Object Relational Mapping
|
||||
* @link www.phpdoctrine.com
|
||||
* @since 1.0
|
||||
* @version $Revision$
|
||||
* @author Joe Simms <joe.simms@websites4.com>
|
||||
*/
|
||||
class Doctrine_Node_NestedSet_PostOrderIterator implements Iterator {}
|
169
draft/Node/NestedSet/PreOrderIterator.php
Normal file
169
draft/Node/NestedSet/PreOrderIterator.php
Normal file
@ -0,0 +1,169 @@
|
||||
<?php
|
||||
/*
|
||||
* $Id$
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* This software consists of voluntary contributions made by many individuals
|
||||
* and is licensed under the LGPL. For more information, see
|
||||
* <http://www.phpdoctrine.com>.
|
||||
*/
|
||||
/**
|
||||
* Doctrine_Node_NestedSet_PreOrderIterator
|
||||
*
|
||||
* @package Doctrine
|
||||
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
||||
* @category Object Relational Mapping
|
||||
* @link www.phpdoctrine.com
|
||||
* @since 1.0
|
||||
* @version $Revision$
|
||||
* @author Joe Simms <joe.simms@websites4.com>
|
||||
*/
|
||||
class Doctrine_Node_NestedSet_PreOrderIterator implements Iterator
|
||||
{
|
||||
/**
|
||||
* @var Doctrine_Collection $collection
|
||||
*/
|
||||
protected $collection;
|
||||
/**
|
||||
* @var array $keys
|
||||
*/
|
||||
protected $keys;
|
||||
/**
|
||||
* @var mixed $key
|
||||
*/
|
||||
protected $key;
|
||||
/**
|
||||
* @var integer $index
|
||||
*/
|
||||
protected $index;
|
||||
/**
|
||||
* @var integer $index
|
||||
*/
|
||||
protected $prevIndex;
|
||||
/**
|
||||
* @var integer $index
|
||||
*/
|
||||
protected $traverseLevel;
|
||||
/**
|
||||
* @var integer $count
|
||||
*/
|
||||
protected $count;
|
||||
|
||||
public function __construct($record, $opts) {
|
||||
|
||||
$componentName = $record->getTable()->getComponentName();
|
||||
|
||||
$q = $record->getTable()->createQuery();
|
||||
|
||||
if(isset($opts['include_record']) && $opts['include_record'])
|
||||
{
|
||||
$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;
|
||||
$this->options = $opts;
|
||||
$this->collection = isset($opts['collection']) ? $opts['collection'] : $query->execute();
|
||||
$this->keys = $this->collection->getKeys();
|
||||
$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();
|
||||
}
|
||||
|
||||
/**
|
||||
* rewinds the iterator
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function rewind() {
|
||||
$this->index = -1;
|
||||
$this->key = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* returns the current key
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function key() {
|
||||
return $this->key;
|
||||
}
|
||||
/**
|
||||
* returns the current record
|
||||
*
|
||||
* @return Doctrine_Record
|
||||
*/
|
||||
public function current() {
|
||||
$record = $this->collection->get($this->key);
|
||||
$record->getNode()->setLevel($this->level);
|
||||
return $record;
|
||||
}
|
||||
/**
|
||||
* advances the internal pointer
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function next() {
|
||||
while($current = $this->advanceIndex())
|
||||
{
|
||||
if($this->maxLevel && ($this->level > $this->maxLevel))
|
||||
continue;
|
||||
|
||||
return $current;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return boolean whether or not the iteration will continue
|
||||
*/
|
||||
public function valid() {
|
||||
return ($this->index < $this->count);
|
||||
}
|
||||
|
||||
public function count() {
|
||||
return $this->count;
|
||||
}
|
||||
|
||||
private function updateLevel() {
|
||||
if(!(isset($this->options['include_record']) && $this->options['include_record'] && $this->index == 0))
|
||||
{
|
||||
$left = $this->collection->get($this->key)->getNode()->getLeftValue();
|
||||
$this->level += $this->prevLeft - $left + 2;
|
||||
$this->prevLeft = $left;
|
||||
}
|
||||
}
|
||||
|
||||
private function advanceIndex() {
|
||||
$this->index++;
|
||||
$i = $this->index;
|
||||
if(isset($this->keys[$i]))
|
||||
{
|
||||
$this->key = $this->keys[$i];
|
||||
$this->updateLevel();
|
||||
return $this->current();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
57
draft/Query/Set.php
Normal file
57
draft/Query/Set.php
Normal file
@ -0,0 +1,57 @@
|
||||
<?php
|
||||
/*
|
||||
* $Id$
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* This software consists of voluntary contributions made by many individuals
|
||||
* and is licensed under the LGPL. For more information, see
|
||||
* <http://www.phpdoctrine.com>.
|
||||
*/
|
||||
Doctrine::autoload('Doctrine_Query_Part');
|
||||
/**
|
||||
* Doctrine_Query
|
||||
*
|
||||
* @package Doctrine
|
||||
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
||||
* @category Object Relational Mapping
|
||||
* @link www.phpdoctrine.com
|
||||
* @since 1.0
|
||||
* @version $Revision$
|
||||
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
||||
*/
|
||||
class Doctrine_Query_Set extends Doctrine_Query_Part
|
||||
{
|
||||
public function parse($dql)
|
||||
{
|
||||
$parts = Doctrine_Query::sqlExplode($dql, ',');
|
||||
|
||||
$result = array();
|
||||
foreach ($parts as $part) {
|
||||
$set = Doctrine_Query::sqlExplode($part, '=');
|
||||
|
||||
$e = explode('.', trim($set[0]));
|
||||
$field = array_pop($e);
|
||||
|
||||
$reference = implode('.', $e);
|
||||
|
||||
$alias = $this->query->getTableAlias($reference);
|
||||
|
||||
$fieldname = $alias ? $alias . '.' . $field : $field;
|
||||
$result[] = $fieldname . ' = ' . $set[1];
|
||||
}
|
||||
|
||||
return implode(', ', $result);
|
||||
}
|
||||
}
|
||||
|
223
draft/Query/Where.php
Normal file
223
draft/Query/Where.php
Normal file
@ -0,0 +1,223 @@
|
||||
<?php
|
||||
/*
|
||||
* $Id$
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* This software consists of voluntary contributions made by many individuals
|
||||
* and is licensed under the LGPL. For more information, see
|
||||
* <http://www.phpdoctrine.com>.
|
||||
*/
|
||||
Doctrine::autoload('Doctrine_Query_Condition');
|
||||
/**
|
||||
* Doctrine_Query_Where
|
||||
*
|
||||
* @package Doctrine
|
||||
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
||||
* @category Object Relational Mapping
|
||||
* @link www.phpdoctrine.com
|
||||
* @since 1.0
|
||||
* @version $Revision$
|
||||
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
||||
*/
|
||||
class Doctrine_Query_Where extends Doctrine_Query_Condition
|
||||
{
|
||||
/**
|
||||
* load
|
||||
* returns the parsed query part
|
||||
*
|
||||
* @param string $where
|
||||
* @return string
|
||||
*/
|
||||
public function load($where)
|
||||
{
|
||||
$where = trim($where);
|
||||
|
||||
$e = Doctrine_Query::sqlExplode($where);
|
||||
|
||||
if (count($e) > 1) {
|
||||
$tmp = $e[0] . ' ' . $e[1];
|
||||
|
||||
if (substr($tmp, 0, 6) == 'EXISTS') {
|
||||
return $this->parseExists($where, true);
|
||||
} elseif (substr($where, 0, 10) == 'NOT EXISTS') {
|
||||
return $this->parseExists($where, false);
|
||||
}
|
||||
}
|
||||
|
||||
if (count($e) < 3) {
|
||||
$e = Doctrine_Query::sqlExplode($where, array('=', '<', '>', '!='));
|
||||
}
|
||||
$r = array_shift($e);
|
||||
|
||||
$a = explode('.', $r);
|
||||
|
||||
if (count($a) > 1) {
|
||||
$field = array_pop($a);
|
||||
$count = count($e);
|
||||
$slice = array_slice($e, -1, 1);
|
||||
$value = implode('', $slice);
|
||||
$operator = trim(substr($where, strlen($r), -strlen($value)));
|
||||
|
||||
$reference = implode('.', $a);
|
||||
$count = count($a);
|
||||
|
||||
$pos = strpos($field, '(');
|
||||
|
||||
if ($pos !== false) {
|
||||
$func = substr($field, 0, $pos);
|
||||
$value = trim(substr($field, ($pos + 1), -1));
|
||||
|
||||
$values = Doctrine_Query::sqlExplode($value, ',');
|
||||
|
||||
$field = array_pop($a);
|
||||
$reference = implode('.', $a);
|
||||
$table = $this->query->load($reference, false);
|
||||
array_pop($a);
|
||||
$reference2 = implode('.', $a);
|
||||
$alias = $this->query->getTableAlias($reference2);
|
||||
|
||||
$stack = $this->query->getRelationStack();
|
||||
$relation = end($stack);
|
||||
|
||||
$stack = $this->query->getTableStack();
|
||||
|
||||
switch ($func) {
|
||||
case 'contains':
|
||||
case 'regexp':
|
||||
case 'like':
|
||||
$operator = $this->getOperator($func);
|
||||
|
||||
if (empty($relation)) {
|
||||
throw new Doctrine_Query_Exception('DQL functions contains/regexp/like can only be used for fields of related components');
|
||||
}
|
||||
$where = array();
|
||||
foreach ($values as $value) {
|
||||
$where[] = $alias.'.'.$relation->getLocal().
|
||||
' IN (SELECT '.$relation->getForeign().
|
||||
' FROM '.$relation->getTable()->getTableName().' WHERE '.$field.$operator.$value.')';
|
||||
}
|
||||
$where = implode(' AND ', $where);
|
||||
break;
|
||||
default:
|
||||
throw new Doctrine_Query_Exception('Unknown DQL function: '.$func);
|
||||
}
|
||||
} else {
|
||||
$table = $this->query->load($reference, false);
|
||||
$alias = $this->query->getTableAlias($reference);
|
||||
$table = $this->query->getTable($alias);
|
||||
// check if value is enumerated value
|
||||
$enumIndex = $table->enumIndex($field, trim($value, "'"));
|
||||
|
||||
if (substr($value, 0, 1) == '(') {
|
||||
// trim brackets
|
||||
$trimmed = Doctrine_Query::bracketTrim($value);
|
||||
|
||||
if (substr($trimmed, 0, 4) == 'FROM' || substr($trimmed, 0, 6) == 'SELECT') {
|
||||
// subquery found
|
||||
$q = new Doctrine_Query();
|
||||
$value = '(' . $q->parseQuery($trimmed)->getQuery() . ')';
|
||||
} elseif (substr($trimmed, 0, 4) == 'SQL:') {
|
||||
$value = '(' . substr($trimmed, 4) . ')';
|
||||
} else {
|
||||
// simple in expression found
|
||||
$e = Doctrine_Query::sqlExplode($trimmed, ',');
|
||||
|
||||
$value = array();
|
||||
foreach ($e as $part) {
|
||||
$index = $table->enumIndex($field, trim($part, "'"));
|
||||
if ($index !== false) {
|
||||
$value[] = $index;
|
||||
} else {
|
||||
$value[] = $this->parseLiteralValue($part);
|
||||
}
|
||||
}
|
||||
$value = '(' . implode(', ', $value) . ')';
|
||||
}
|
||||
} else {
|
||||
if ($enumIndex !== false) {
|
||||
$value = $enumIndex;
|
||||
} else {
|
||||
$value = $this->parseLiteralValue($value);
|
||||
}
|
||||
}
|
||||
|
||||
switch ($operator) {
|
||||
case '<':
|
||||
case '>':
|
||||
case '=':
|
||||
case '!=':
|
||||
if ($enumIndex !== false) {
|
||||
$value = $enumIndex;
|
||||
}
|
||||
default:
|
||||
$fieldname = $alias ? $alias . '.' . $field : $field;
|
||||
$where = $fieldname . ' '
|
||||
. $operator . ' ' . $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $where;
|
||||
}
|
||||
/**
|
||||
* parses an EXISTS expression
|
||||
*
|
||||
* @param string $where query where part to be parsed
|
||||
* @param boolean $negation whether or not to use the NOT keyword
|
||||
* @return string
|
||||
*/
|
||||
public function parseExists($where, $negation)
|
||||
{
|
||||
$operator = ($negation) ? 'EXISTS' : 'NOT EXISTS';
|
||||
|
||||
$pos = strpos($where, '(');
|
||||
|
||||
if ($pos == false)
|
||||
throw new Doctrine_Query_Exception("Unknown expression, expected '('");
|
||||
|
||||
$sub = Doctrine_Query::bracketTrim(substr($where, $pos));
|
||||
|
||||
return $operator . ' ('.$this->query->createSubquery()->parseQuery($sub, false)->getQuery() . ')';
|
||||
}
|
||||
/**
|
||||
* getOperator
|
||||
*
|
||||
* @param string $func
|
||||
* @return string
|
||||
*/
|
||||
public function getOperator($func)
|
||||
{
|
||||
switch ($func) {
|
||||
case 'contains':
|
||||
$operator = ' = ';
|
||||
break;
|
||||
case 'regexp':
|
||||
$operator = $this->query->getConnection()->getRegexpOperator();
|
||||
break;
|
||||
case 'like':
|
||||
$operator = ' LIKE ';
|
||||
break;
|
||||
}
|
||||
return $operator;
|
||||
}
|
||||
/**
|
||||
* __toString
|
||||
* return string representation of this object
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
return ( ! empty($this->parts))?implode(' AND ', $this->parts):'';
|
||||
}
|
||||
}
|
72
draft/README.tree
Normal file
72
draft/README.tree
Normal file
@ -0,0 +1,72 @@
|
||||
REMEMBER
|
||||
--------
|
||||
If performing batch tree manipulation tasks, then remember to refresh your records (see record::refresh()), as any transformations of the tree are likely to affect all instances of records that you have in your scope. (zYne, is there any way of automating this?)
|
||||
|
||||
If you are inserting or moving a node within the tree, you must use the appropriate function to place the node in it's destination. Note: you cannot save a new record without inserting it into the tree.
|
||||
|
||||
You can save an already existing node using record::save() without affecting the tree. Never set the tree specific record attributes manually.
|
||||
|
||||
If you wish to delete a record, you MUST delete the node and not the record, using $record->deleteNode() or $record->getNode()->delete(). Deleting a node, will delete all its descendants.
|
||||
|
||||
The difference between descendants and children is that descendants include children of children whereas children are direct descendants of their parent (real children not gran children and great gran children etc etc).
|
||||
|
||||
The most effective way to traverse a tree from the root node, is to use the tree::fetchTree() method:
|
||||
$tree = $manager->getTable('Model')->getTree()->fetchTree();
|
||||
It will by default include the root node in the tree and will return an iterator to traverse the tree.
|
||||
|
||||
To traverse a tree from a given node, it will normally cost 3 queries, one to fetch the starting node, one to fetch the branch from this node, and one to determine the level of the start node, the traversal algorithm with then determine the level of each subsequent node for you.
|
||||
|
||||
EXAMPLES
|
||||
--------
|
||||
See EXAMPLE.tree.php for very draft examples on how to use the tree interface within doctrine (note that the interface is independent of the implementation, so when other implementations are added, you should be able to switch implementation and your code still work). This should be run in a browser and relevant database settings altered as appropriate.
|
||||
|
||||
MORE INFO
|
||||
---------
|
||||
For more info on storing hierarchical data in databases, the various implementations and tree traversal see these articles:
|
||||
|
||||
http://dev.mysql.com/tech-resources/articles/hierarchical-data.html
|
||||
http://www.sitepoint.com/article/hierarchical-data-database
|
||||
http://en.wikipedia.org/wiki/Tree_traversal
|
||||
|
||||
TO DO
|
||||
-----
|
||||
Discuss adding __call() to record to allow the node methods to be called directly from the record, although i know we are trying to avoid introspection.
|
||||
|
||||
Discuss adding a level column to the database to store levels (will reduce traversing nodes by one query, and allow us to implement the LevelOrder Traversal of the Tree with one query, but updating tree may be more costly).
|
||||
|
||||
Maybe add tree configuration to allow the above to be configurable as well as other options such as:
|
||||
-on deletion of a node, move descendants, unassign descendants or delete descendants
|
||||
-allowing the ability to save nodes that are not assigned in the tree (set lft=0, rgt=0, retrieve with tree::fetchUnassigned)
|
||||
-auto refreshing objects left and right values used in tree transformations
|
||||
|
||||
Return getSiblings and getAncestors as Iterators
|
||||
|
||||
NOTES FOR ZYNE
|
||||
--------------
|
||||
|
||||
IMHO, i think that the Ajacency list should be moved into a tree structure, the table definitions and setUp can be set in the class Doctrine_Tree_AdjacencyList, then they simply have to call actsAsTree('AdjacencyList') in their setTableDefinition, although to be honest, with nestedset implemented i cannot think why i would want to use adjacency list anyhow !
|
||||
|
||||
Doctrine_Query_Set, Doctrine_Query_Where
|
||||
----------------------------------------
|
||||
Not too sure if problem with my query syntax or with Doctrine, but i followed examples in docs
|
||||
In set and where, if not Component supplied, then alias is empty, so resultant query would be something like:
|
||||
UPDATE menu m SET .lft = lft + 2
|
||||
WHERE lft >= ?
|
||||
Notice the invalid resultant syntax .lft, as the alias was not set, so added check to determine if alias isset.
|
||||
|
||||
CHANGELOG
|
||||
---------
|
||||
|
||||
Doctrine_Record
|
||||
---------------
|
||||
added methods:
|
||||
getNode()
|
||||
deleteNode()
|
||||
|
||||
amended:
|
||||
actsAsTree()
|
||||
|
||||
Doctrine_Table
|
||||
--------------
|
||||
amended constructor to call tree::setUp()
|
||||
added setTree(), to call tree::setTableDefinition() and setup Tree in Table
|
1465
draft/Record.php
Normal file
1465
draft/Record.php
Normal file
File diff suppressed because it is too large
Load Diff
1217
draft/Table.php
Normal file
1217
draft/Table.php
Normal file
File diff suppressed because it is too large
Load Diff
@ -21,23 +21,69 @@
|
||||
/**
|
||||
* Doctrine_Tree
|
||||
*
|
||||
* the purpose of Doctrine_Tree is to provide tree access
|
||||
* functionality for all records extending it
|
||||
*
|
||||
* @package Doctrine ORM
|
||||
* @url www.phpdoctrine.com
|
||||
* @license LGPL
|
||||
* @package Doctrine
|
||||
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
||||
* @category Object Relational Mapping
|
||||
* @link www.phpdoctrine.com
|
||||
* @since 1.0
|
||||
* @version $Revision$
|
||||
* @author Joe Simms <joe.simms@websites4.com>
|
||||
*/
|
||||
abstract class Doctrine_Tree extends Doctrine_Record {
|
||||
class Doctrine_Tree
|
||||
{
|
||||
/**
|
||||
* @param object $table reference to associated Doctrine_Table instance
|
||||
*/
|
||||
protected $table;
|
||||
|
||||
abstract public function getLeafNodes();
|
||||
/**
|
||||
* @param array $options
|
||||
*/
|
||||
protected $options;
|
||||
|
||||
abstract public function getPath();
|
||||
/**
|
||||
* contructor, creates tree with reference to table and any options
|
||||
*
|
||||
* @param object $table instance of Doctrine_Table
|
||||
* @param array $options options
|
||||
*/
|
||||
public function __construct($table, $options)
|
||||
{
|
||||
$this->table = $table;
|
||||
$this->options = $options;
|
||||
}
|
||||
|
||||
abstract public function getDepth();
|
||||
/**
|
||||
* Used to define table attributes required for the given implementation
|
||||
*
|
||||
*/
|
||||
public function setTableDefinition()
|
||||
{
|
||||
throw new Doctrine_Tree_Exception('Table attributes have not been defined for this Tree implementation.');
|
||||
}
|
||||
|
||||
abstract public function removeNode();
|
||||
/**
|
||||
* this method is used for setting up relations and attributes and should be used by specific implementations
|
||||
*
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
|
||||
abstract public function addNode();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* factory method to return tree instance based upon chosen implementation
|
||||
*
|
||||
* @param object $table instance of Doctrine_Table
|
||||
* @param string $impName implementation (NestedSet, AdjacencyList, MaterializedPath)
|
||||
* @param array $options options
|
||||
* @return object $options instance of Doctrine_Node
|
||||
*/
|
||||
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);
|
||||
}
|
||||
} // END class
|
32
draft/Tree/AdjacencyList.php
Normal file
32
draft/Tree/AdjacencyList.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
/*
|
||||
* $Id$
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* This software consists of voluntary contributions made by many individuals
|
||||
* and is licensed under the LGPL. For more information, see
|
||||
* <http://www.phpdoctrine.com>.
|
||||
*/
|
||||
/**
|
||||
* Doctrine_Tree_AdjacencyList
|
||||
*
|
||||
* @package Doctrine
|
||||
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
||||
* @category Object Relational Mapping
|
||||
* @link www.phpdoctrine.com
|
||||
* @since 1.0
|
||||
* @version $Revision$
|
||||
* @author Joe Simms <joe.simms@websites4.com>
|
||||
*/
|
||||
class Doctrine_Tree_AdjacencyList extends Doctrine_Tree implements Doctrine_Tree_Interface { }
|
32
draft/Tree/Exception.php
Normal file
32
draft/Tree/Exception.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
/*
|
||||
* $Id$
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* This software consists of voluntary contributions made by many individuals
|
||||
* and is licensed under the LGPL. For more information, see
|
||||
* <http://www.phpdoctrine.com>.
|
||||
*/
|
||||
/**
|
||||
* Doctrine_Tree_Exception
|
||||
*
|
||||
* @package Doctrine
|
||||
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
||||
* @category Object Relational Mapping
|
||||
* @link www.phpdoctrine.com
|
||||
* @since 1.0
|
||||
* @version $Revision$
|
||||
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
||||
*/
|
||||
class Doctrine_Tree_Exception extends Doctrine_Exception { }
|
64
draft/Tree/Interface.php
Normal file
64
draft/Tree/Interface.php
Normal file
@ -0,0 +1,64 @@
|
||||
<?php
|
||||
/*
|
||||
* $Id$
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* This software consists of voluntary contributions made by many individuals
|
||||
* and is licensed under the LGPL. For more information, see
|
||||
* <http://www.phpdoctrine.com>.
|
||||
*/
|
||||
/**
|
||||
* Doctrine_Tree_Interface
|
||||
*
|
||||
* @package Doctrine
|
||||
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
||||
* @category Object Relational Mapping
|
||||
* @link www.phpdoctrine.com
|
||||
* @since 1.0
|
||||
* @version $Revision$
|
||||
* @author Joe Simms <joe.simms@websites4.com>
|
||||
*/
|
||||
interface Doctrine_Tree_Interface {
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
||||
/**
|
||||
* returns root node
|
||||
*
|
||||
* @return object $record instance of Doctrine_Record
|
||||
*/
|
||||
public function findRoot();
|
||||
|
||||
/**
|
||||
* optimised method to returns iterator for traversal of the entire tree from root
|
||||
*
|
||||
* @param array $options options
|
||||
* @return object $iterator instance of Doctrine_Node_<Implementation>_PreOrderIterator
|
||||
*/
|
||||
public function fetchTree($options = array());
|
||||
|
||||
/**
|
||||
* 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());
|
||||
}
|
32
draft/Tree/MaterializedPath.php
Normal file
32
draft/Tree/MaterializedPath.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
/*
|
||||
* $Id$
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* This software consists of voluntary contributions made by many individuals
|
||||
* and is licensed under the LGPL. For more information, see
|
||||
* <http://www.phpdoctrine.com>.
|
||||
*/
|
||||
/**
|
||||
* Doctrine_Tree_MaterializedPath
|
||||
*
|
||||
* @package Doctrine
|
||||
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
||||
* @category Object Relational Mapping
|
||||
* @link www.phpdoctrine.com
|
||||
* @since 1.0
|
||||
* @version $Revision$
|
||||
* @author Joe Simms <joe.simms@websites4.com>
|
||||
*/
|
||||
class Doctrine_Tree_MaterializedPath extends Doctrine_Tree implements Doctrine_Tree_Interface { }
|
143
draft/Tree/NestedSet.php
Normal file
143
draft/Tree/NestedSet.php
Normal file
@ -0,0 +1,143 @@
|
||||
<?php
|
||||
/*
|
||||
* $Id$
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* This software consists of voluntary contributions made by many individuals
|
||||
* and is licensed under the LGPL. For more information, see
|
||||
* <http://www.phpdoctrine.com>.
|
||||
*/
|
||||
/**
|
||||
* Doctrine_Tree_NestedSet
|
||||
*
|
||||
* @package Doctrine
|
||||
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
||||
* @category Object Relational Mapping
|
||||
* @link www.phpdoctrine.com
|
||||
* @since 1.0
|
||||
* @version $Revision$
|
||||
* @author Joe Simms <joe.simms@websites4.com>
|
||||
*/
|
||||
class Doctrine_Tree_NestedSet extends Doctrine_Tree implements Doctrine_Tree_Interface
|
||||
{
|
||||
/**
|
||||
* used to define table attributes required for the NestetSet implementation
|
||||
* adds lft and rgt columns for corresponding left and right values
|
||||
*
|
||||
*/
|
||||
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();
|
||||
}
|
||||
|
||||
$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);
|
||||
|
||||
return $root;
|
||||
}
|
||||
/**
|
||||
* 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($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);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user