/** * 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(); }
/** * The returned array will contain objects of the default type or * objects that inherit from the default. * * @param DataFetcherInterface $dataFetcher * @return array * @throws PropelException Any exceptions caught during processing will be * rethrown wrapped into a PropelException. */ public static function populateObjects(DataFetcherInterface $dataFetcher) { $results = array(); // set the class once to avoid overhead in the loop $cls = static::getOMClass(false); // populate the object(s) while ($row = $dataFetcher->fetch()) { $key = CategoryTableMap::getPrimaryKeyHashFromRow($row, 0, $dataFetcher->getIndexType()); if (null !== ($obj = CategoryTableMap::getInstanceFromPool($key))) { // We no longer rehydrate the object, since this can cause data loss. // See http://www.propelorm.org/ticket/509 // $obj->hydrate($row, 0, true); // rehydrate $results[] = $obj; } else { /** @var Category $obj */ $obj = new $cls(); $obj->hydrate($row); $results[] = $obj; CategoryTableMap::addInstanceToPool($obj, $key); } // if key exists } return $results; }
/** * Persists this object to the database. * * If the object is new, it inserts it; otherwise an update is performed. * All modified related objects will also be persisted in the doSave() * method. This method wraps all precipitate database operations in a * single transaction. * * @param ConnectionInterface $con * @return int The number of rows affected by this insert/update and any referring fk objects' save() operations. * @throws PropelException * @see doSave() */ public function save(ConnectionInterface $con = null) { if ($this->isDeleted()) { throw new PropelException("You cannot save an object that has been deleted."); } if ($con === null) { $con = Propel::getServiceContainer()->getWriteConnection(CategoryTableMap::DATABASE_NAME); } return $con->transaction(function () use($con) { $isInsert = $this->isNew(); $ret = $this->preSave($con); // nested_set behavior if ($this->isNew() && $this->isRoot()) { // check if no other root exist in, the tree $rootExists = ChildCategoryQuery::create()->addUsingAlias(ChildCategory::LEFT_COL, 1, Criteria::EQUAL)->exists($con); if ($rootExists) { throw new PropelException('A root node already exists in this tree. To allow multiple root nodes, add the `use_scope` parameter in the nested_set behavior tag.'); } } $this->processNestedSetQueries($con); if ($isInsert) { $ret = $ret && $this->preInsert($con); // timestampable behavior if (!$this->isColumnModified(CategoryTableMap::COL_CREATED_AT)) { $this->setCreatedAt(time()); } if (!$this->isColumnModified(CategoryTableMap::COL_UPDATED_AT)) { $this->setUpdatedAt(time()); } } else { $ret = $ret && $this->preUpdate($con); // timestampable behavior if ($this->isModified() && !$this->isColumnModified(CategoryTableMap::COL_UPDATED_AT)) { $this->setUpdatedAt(time()); } } if ($ret) { $affectedRows = $this->doSave($con); if ($isInsert) { $this->postInsert($con); } else { $this->postUpdate($con); } $this->postSave($con); CategoryTableMap::addInstanceToPool($this); } else { $affectedRows = 0; } return $affectedRows; }); }