Changeset 1572
- Timestamp:
- 21.02.2010 23:24:20 (5 months ago)
- Location:
- branches/1.5
- Files:
-
- 2 modified
Legend:
- Unmodified
- Added
- Removed
-
branches/1.5/generator/lib/behavior/nestedset/NestedSetBehaviorObjectBuilderModifier.php
r1571 r1572 83 83 * @var null|PropelObjectCollection 84 84 */ 85 protected \$nestedSetChildren = null; 85 protected \$collNestedSetChildren = null; 86 87 /** 88 * Internal cache for parent node 89 * @var null|$objectClassname 90 */ 91 protected \$aNestedSetParent = null; 92 86 93 "; 87 94 } … … 112 119 public function objectClearReferences($builder) 113 120 { 114 return "\$this->clearNestedSetChildren();"; 121 return "\$this->collNestedSetChildren = null; 122 \$this->aNestedSetParent = null;"; 115 123 } 116 124 … … 147 155 148 156 $this->addHasParent($script); 157 $this->addSetParent($script); 149 158 $this->addGetParent($script); 150 159 … … 459 468 } 460 469 470 protected function addSetParent(&$script) 471 { 472 $objectClassname = $this->objectClassname; 473 $script .= " 474 /** 475 * Sets the cache for parent node of the current object. 476 * Warning: this does not move the current object in the tree. 477 * Use moveTofirstChildOf() or moveToLastChildOf() for that purpose 478 * 479 * @param $objectClassname \$parent 480 * @return $objectClassname The current object, for fluid interface 481 */ 482 public function setParent(\$parent = null) 483 { 484 \$this->aNestedSetParent = \$parent; 485 return \$this; 486 } 487 "; 488 } 489 490 461 491 protected function addGetParent(&$script) 462 492 { 463 493 $script .= " 464 494 /** 465 * Gets ancestor for the given nodeif it exists495 * Gets parent node for the current object if it exists 466 496 * The result is cached so further calls to the same method don't issue any queries 467 497 * … … 471 501 public function getParent(PropelPDO \$con = null) 472 502 { 473 if(!\$this->hasParent()) { 474 return null; 475 } else { 476 return {$this->queryClassname}::create() 503 if (\$this->aNestedSetParent === null && \$this->hasParent()) { 504 \$this->aNestedSetParent = {$this->queryClassname}::create() 477 505 ->ancestorsOf(\$this) 478 506 ->orderByLevel(true) 479 ->findOne(); 480 } 507 ->findOne(\$con); 508 } 509 return \$this->aNestedSetParent; 481 510 } 482 511 "; … … 591 620 $script .= " 592 621 /** 593 * Clears out the \$ nestedSetChildren collection622 * Clears out the \$collNestedSetChildren collection 594 623 * 595 624 * This does not modify the database; however, it will remove any associated objects, causing … … 600 629 public function clearNestedSetChildren() 601 630 { 602 \$this-> nestedSetChildren = null;631 \$this->collNestedSetChildren = null; 603 632 } 604 633 "; … … 609 638 $script .= " 610 639 /** 611 * Initializes the \$ nestedSetChildren collection.640 * Initializes the \$collNestedSetChildren collection. 612 641 * 613 642 * @return void … … 615 644 public function initNestedSetChildren() 616 645 { 617 \$this-> nestedSetChildren = new PropelObjectCollection();618 \$this-> nestedSetChildren->setModel('" . $this->builder->getNewStubObjectBuilder($this->table)->getClassname() . "');646 \$this->collNestedSetChildren = new PropelObjectCollection(); 647 \$this->collNestedSetChildren->setModel('" . $this->builder->getNewStubObjectBuilder($this->table)->getClassname() . "'); 619 648 } 620 649 "; … … 627 656 $script .= " 628 657 /** 629 * Adds an element to the internal \$ nestedSetChildren collection.658 * Adds an element to the internal \$collNestedSetChildren collection. 630 659 * Beware that this doesn't insert a node in the tree. 631 660 * This method is only used to facilitate children hydration. … … 637 666 public function addNestedSetChild($objectName) 638 667 { 639 if (\$this-> nestedSetChildren === null) {668 if (\$this->collNestedSetChildren === null) { 640 669 \$this->initNestedSetChildren(); 641 670 } 642 if (!\$this-> nestedSetChildren->contains($objectName)) { // only add it if the **same** object is not already associated643 \$this-> nestedSetChildren[]= $objectName;644 //{$objectName}->setParent(\$this);671 if (!\$this->collNestedSetChildren->contains($objectName)) { // only add it if the **same** object is not already associated 672 \$this->collNestedSetChildren[]= $objectName; 673 {$objectName}->setParent(\$this); 645 674 } 646 675 } … … 678 707 public function getChildren(\$criteria = null, PropelPDO \$con = null) 679 708 { 680 if(null === \$this-> nestedSetChildren || null !== \$criteria) {681 if (\$this->isLeaf() || (\$this->isNew() && null === \$this-> nestedSetChildren)) {709 if(null === \$this->collNestedSetChildren || null !== \$criteria) { 710 if (\$this->isLeaf() || (\$this->isNew() && null === \$this->collNestedSetChildren)) { 682 711 // return empty collection 683 712 \$this->initNestedSetChildren(); 684 713 } else { 685 \$ nestedSetChildren = $queryClassname::create(null, \$criteria)714 \$collNestedSetChildren = $queryClassname::create(null, \$criteria) 686 715 ->childrenOf(\$this) 687 716 ->orderByBranch() 688 717 ->find(\$con); 689 718 if (null !== \$criteria) { 690 return \$ nestedSetChildren;719 return \$collNestedSetChildren; 691 720 } 692 \$this-> nestedSetChildren = \$nestedSetChildren;693 } 694 } 695 return \$this-> nestedSetChildren;721 \$this->collNestedSetChildren = \$collNestedSetChildren; 722 } 723 } 724 return \$this->collNestedSetChildren; 696 725 } 697 726 "; … … 713 742 public function countChildren(\$criteria = null, PropelPDO \$con = null) 714 743 { 715 if(null === \$this-> nestedSetChildren || null !== \$criteria) {716 if (\$this->isLeaf() || (\$this->isNew() && null === \$this-> nestedSetChildren)) {744 if(null === \$this->collNestedSetChildren || null !== \$criteria) { 745 if (\$this->isLeaf() || (\$this->isNew() && null === \$this->collNestedSetChildren)) { 717 746 return 0; 718 747 } else { … … 722 751 } 723 752 } else { 724 return count(\$this-> nestedSetChildren);753 return count(\$this->collNestedSetChildren); 725 754 } 726 755 } -
branches/1.5/test/testsuite/generator/behavior/nestedset/NestedSetBehaviorObjectBuilderModifierTest.php
r1571 r1572 286 286 } 287 287 288 public function testGetParentCache() 289 { 290 list($t1, $t2, $t3, $t4, $t5, $t6, $t7) = $this->initTree(); 291 /* Tree used for tests 292 t1 293 | \ 294 t2 t3 295 | \ 296 t4 t5 297 | \ 298 t6 t7 299 */ 300 $con = Propel::getConnection(); 301 $count = $con->getQueryCount(); 302 $parent = $t5->getParent($con); 303 $parent = $t5->getParent($con); 304 $this->assertEquals($count + 1, $con->getQueryCount(), 'getParent() only issues a query once'); 305 $this->assertEquals('t3', $parent->getTitle(), 'getParent() returns the parent Node'); 306 } 307 288 308 public function testHasPrevSibling() 289 309 { … … 354 374 $this->assertEquals($t6->getNextSibling($this->con), $t7, 'getNextSibling() correctly retrieves next sibling'); 355 375 $this->assertNull($t7->getNextSibling($this->con), 'getNextSibling() returns null for last siblings'); 376 } 377 378 public function testAddNestedSetChildren() 379 { 380 $t0 = new Table9(); 381 $t1 = new Table9(); 382 $t2 = new Table9(); 383 $t0->addNestedSetChild($t1); 384 $t0->addNestedSetChild($t2); 385 $this->assertEquals(2, $t0->countChildren(), 'addNestedSetChild() adds the object to the internal children collection'); 386 $this->assertEquals($t0, $t1->getParent(), 'addNestedSetChild() sets the object as th parent of the parameter'); 387 $this->assertEquals($t0, $t2->getParent(), 'addNestedSetChild() sets the object as th parent of the parameter'); 356 388 } 357 389