/** * If this collection has already been initialized with * an identical criteria, it returns the collection. * Otherwise if this File is new, it will return * an empty collection; or if this File has previously * been saved, it will retrieve related Categories from storage. * * This method is protected by default in order to keep the public * api reasonable. You can provide public methods for those you * actually need in File. * * @param Criteria $criteria optional Criteria object to narrow the query * @param ConnectionInterface $con optional connection object * @param string $joinBehavior optional join type to use (defaults to Criteria::LEFT_JOIN) * @return ObjectCollection|ChildCategory[] List of ChildCategory objects */ public function getCategoriesJoinResource(Criteria $criteria = null, ConnectionInterface $con = null, $joinBehavior = Criteria::LEFT_JOIN) { $query = ChildCategoryQuery::create(null, $criteria); $query->joinWith('Resource', $joinBehavior); return $this->getCategories($query, $con); }
/** * Performs an INSERT on the database, given a Category or Criteria object. * * @param mixed $criteria Criteria or Category object containing data that is used to create the INSERT statement. * @param ConnectionInterface $con the ConnectionInterface connection to use * @return mixed The new primary key. * @throws PropelException Any exceptions caught during processing will be * rethrown wrapped into a PropelException. */ public static function doInsert($criteria, ConnectionInterface $con = null) { if (null === $con) { $con = Propel::getServiceContainer()->getWriteConnection(CategoryTableMap::DATABASE_NAME); } if ($criteria instanceof Criteria) { $criteria = clone $criteria; // rename for clarity } else { $criteria = $criteria->buildCriteria(); // build Criteria from Category object } if ($criteria->containsKey(CategoryTableMap::COL_CATEGORY_ID) && $criteria->keyContainsValue(CategoryTableMap::COL_CATEGORY_ID)) { throw new PropelException('Cannot insert a value for auto-increment primary key (' . CategoryTableMap::COL_CATEGORY_ID . ')'); } // Set the correct dbName $query = CategoryQuery::create()->mergeWith($criteria); // use transaction because $criteria could contain info // for more than one table (I guess, conceivably) return $con->transaction(function () use($con, $query) { return $query->doInsert($con); }); }
/** * Deletes all descendants for the given node * Instance pooling is wiped out by this command, * so existing ChildCategory instances are probably invalid (except for the current one) * * @param ConnectionInterface $con Connection to use. * * @return int number of deleted nodes */ public function deleteDescendants(ConnectionInterface $con = null) { if ($this->isLeaf()) { // save one query return; } if (null === $con) { $con = Propel::getServiceContainer()->getReadConnection(CategoryTableMap::DATABASE_NAME); } $left = $this->getLeftValue(); $right = $this->getRightValue(); return $con->transaction(function () use($con, $left, $right) { // delete descendant nodes (will empty the instance pool) $ret = ChildCategoryQuery::create()->descendantsOf($this)->delete($con); // fill up the room that was used by descendants ChildCategoryQuery::shiftRLValues($left - $right + 1, $right, null, $con); // fix the right value for the current node, which is now a leaf $this->setRightValue($left + 1); return $ret; }); }
/** * 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(); }
/** * Get the associated ChildCategory object * * @param ConnectionInterface $con Optional Connection object. * @return ChildCategory The associated ChildCategory object. * @throws PropelException */ public function getCategory(ConnectionInterface $con = null) { if ($this->aCategory === null && $this->category_id !== null) { $this->aCategory = ChildCategoryQuery::create()->findPk($this->category_id, $con); /* The following can be used additionally to guarantee the related object contains a reference to this object. This level of coupling may, however, be undesirable since it could result in an only partially populated collection in the referenced object. $this->aCategory->addProducts($this); */ } return $this->aCategory; }