예제 #1
0
 /**
  * Update the tree to allow insertion of a leaf at the specified position
  *
  * @param      ConnectionInterface $con    Connection to use.
  */
 public static function fixLevels(ConnectionInterface $con = null)
 {
     $c = new Criteria();
     $c->addAscendingOrderByColumn(ChildCategory::LEFT_COL);
     $dataFetcher = ChildCategoryQuery::create(null, $c)->setFormatter(ModelCriteria::FORMAT_STATEMENT)->find($con);
     // set the class once to avoid overhead in the loop
     $cls = CategoryTableMap::getOMClass(false);
     $level = null;
     // iterate over the statement
     while ($row = $dataFetcher->fetch()) {
         // hydrate object
         $key = CategoryTableMap::getPrimaryKeyHashFromRow($row, 0);
         /** @var $obj ChildCategory */
         if (null === ($obj = CategoryTableMap::getInstanceFromPool($key))) {
             $obj = new $cls();
             $obj->hydrate($row);
             CategoryTableMap::addInstanceToPool($obj, $key);
         }
         // compute level
         // Algorithm shamelessly stolen from sfPropelActAsNestedSetBehaviorPlugin
         // Probably authored by Tristan Rivoallan
         if ($level === null) {
             $level = 0;
             $i = 0;
             $prev = array($obj->getRightValue());
         } else {
             while ($obj->getRightValue() > $prev[$i]) {
                 $i--;
             }
             $level = ++$i;
             $prev[$i] = $obj->getRightValue();
         }
         // update level in node if necessary
         if ($obj->getLevel() !== $level) {
             $obj->setLevel($level);
             $obj->save($con);
         }
     }
     $dataFetcher->close();
 }