Changeset 1572

Show
Ignore:
Timestamp:
21.02.2010 23:24:20 (5 months ago)
Author:
francois
Message:

[1.5] Added cache for getParent() (refs #854)

Location:
branches/1.5
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • branches/1.5/generator/lib/behavior/nestedset/NestedSetBehaviorObjectBuilderModifier.php

    r1571 r1572  
    8383 * @var        null|PropelObjectCollection 
    8484 */ 
    85 protected \$nestedSetChildren = null; 
     85protected \$collNestedSetChildren = null; 
     86 
     87/** 
     88 * Internal cache for parent node 
     89 * @var        null|$objectClassname 
     90 */ 
     91protected \$aNestedSetParent = null; 
     92 
    8693"; 
    8794        } 
     
    112119        public function objectClearReferences($builder) 
    113120        { 
    114                 return "\$this->clearNestedSetChildren();"; 
     121                return "\$this->collNestedSetChildren = null; 
     122\$this->aNestedSetParent = null;"; 
    115123        } 
    116124         
     
    147155                 
    148156                $this->addHasParent($script); 
     157                $this->addSetParent($script); 
    149158                $this->addGetParent($script); 
    150159                 
     
    459468        } 
    460469 
     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 */ 
     482public function setParent(\$parent = null) 
     483{ 
     484        \$this->aNestedSetParent = \$parent; 
     485        return \$this; 
     486} 
     487"; 
     488        } 
     489 
     490 
    461491        protected function addGetParent(&$script) 
    462492        { 
    463493                $script .= " 
    464494/** 
    465  * Gets ancestor for the given node if it exists 
     495 * Gets parent node for the current object if it exists 
    466496 * The result is cached so further calls to the same method don't issue any queries 
    467497 * 
     
    471501public function getParent(PropelPDO \$con = null) 
    472502{ 
    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() 
    477505                        ->ancestorsOf(\$this) 
    478506                        ->orderByLevel(true) 
    479                         ->findOne(); 
    480         } 
     507                        ->findOne(\$con); 
     508        } 
     509        return \$this->aNestedSetParent; 
    481510} 
    482511"; 
     
    591620                $script .= " 
    592621/** 
    593  * Clears out the \$nestedSetChildren collection 
     622 * Clears out the \$collNestedSetChildren collection 
    594623 * 
    595624 * This does not modify the database; however, it will remove any associated objects, causing 
     
    600629public function clearNestedSetChildren() 
    601630{ 
    602         \$this->nestedSetChildren = null; 
     631        \$this->collNestedSetChildren = null; 
    603632} 
    604633"; 
     
    609638                $script .= " 
    610639/** 
    611  * Initializes the \$nestedSetChildren collection. 
     640 * Initializes the \$collNestedSetChildren collection. 
    612641 * 
    613642 * @return     void 
     
    615644public function initNestedSetChildren() 
    616645{ 
    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() . "'); 
    619648} 
    620649"; 
     
    627656                $script .= " 
    628657/** 
    629  * Adds an element to the internal \$nestedSetChildren collection. 
     658 * Adds an element to the internal \$collNestedSetChildren collection. 
    630659 * Beware that this doesn't insert a node in the tree. 
    631660 * This method is only used to facilitate children hydration. 
     
    637666public function addNestedSetChild($objectName) 
    638667{ 
    639         if (\$this->nestedSetChildren === null) { 
     668        if (\$this->collNestedSetChildren === null) { 
    640669                \$this->initNestedSetChildren(); 
    641670        } 
    642         if (!\$this->nestedSetChildren->contains($objectName)) { // only add it if the **same** object is not already associated 
    643                 \$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); 
    645674        } 
    646675} 
     
    678707public function getChildren(\$criteria = null, PropelPDO \$con = null) 
    679708{ 
    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)) { 
    682711                        // return empty collection 
    683712                        \$this->initNestedSetChildren(); 
    684713                } else { 
    685                         \$nestedSetChildren = $queryClassname::create(null, \$criteria) 
     714                        \$collNestedSetChildren = $queryClassname::create(null, \$criteria) 
    686715                        ->childrenOf(\$this) 
    687716                        ->orderByBranch() 
    688717                                ->find(\$con); 
    689718                        if (null !== \$criteria) { 
    690                                 return \$nestedSetChildren; 
     719                                return \$collNestedSetChildren; 
    691720                        } 
    692                         \$this->nestedSetChildren = \$nestedSetChildren; 
    693                 } 
    694         } 
    695         return \$this->nestedSetChildren; 
     721                        \$this->collNestedSetChildren = \$collNestedSetChildren; 
     722                } 
     723        } 
     724        return \$this->collNestedSetChildren; 
    696725} 
    697726"; 
     
    713742public function countChildren(\$criteria = null, PropelPDO \$con = null) 
    714743{ 
    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)) { 
    717746                        return 0; 
    718747                } else { 
     
    722751                } 
    723752        } else { 
    724                 return count(\$this->nestedSetChildren); 
     753                return count(\$this->collNestedSetChildren); 
    725754        } 
    726755} 
  • branches/1.5/test/testsuite/generator/behavior/nestedset/NestedSetBehaviorObjectBuilderModifierTest.php

    r1571 r1572  
    286286        } 
    287287 
     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 
    288308        public function testHasPrevSibling() 
    289309        { 
     
    354374                $this->assertEquals($t6->getNextSibling($this->con), $t7, 'getNextSibling() correctly retrieves next sibling'); 
    355375                $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'); 
    356388        } 
    357389