EXAMPLE.tree.php 9.64 KB
Newer Older
1 2 3
<?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*/

4
require_once("../lib/Doctrine.php");
5 6
 
// autoloading objects, modified function to search drafts folder first, should run this test script from the drafts folder
7
spl_autoload_register(array('Doctrine', 'autoload'));
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

// 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);

    }
    
23 24
    // this __toString() function is used to get the name for the path, see node::getPath
    public function __toString() {
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
        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 />';
}