protected function addFixLevels(&$script) { $objectClassName = $this->objectClassName; $queryClassName = $this->queryClassName; $tableMapClassName = $this->tableMapClassName; $useScope = $this->behavior->useScope(); $script .= "\n/**\n * Update the tree to allow insertion of a leaf at the specified position\n *"; if ($useScope) { $script .= "\n * @param integer \$scope scope column value"; } $script .= "\n * @param ConnectionInterface \$con Connection to use.\n */\nstatic public function fixLevels(" . ($useScope ? "\$scope, " : "") . "ConnectionInterface \$con = null)\n{\n \$c = new Criteria();"; if ($useScope) { $script .= "\n \$c->add({$objectClassName}::SCOPE_COL, \$scope, Criteria::EQUAL);"; } $script .= "\n \$c->addAscendingOrderByColumn({$objectClassName}::LEFT_COL);\n \$dataFetcher = {$queryClassName}::create(null, \$c)->setFormatter(ModelCriteria::FORMAT_STATEMENT)->find(\$con);\n "; if (!$this->table->getChildrenColumn()) { $script .= "\n // set the class once to avoid overhead in the loop\n \$cls = {$tableMapClassName}::getOMClass(false);"; } $script .= "\n \$level = null;\n // iterate over the statement\n while (\$row = \$dataFetcher->fetch()) {\n\n // hydrate object\n \$key = {$tableMapClassName}::getPrimaryKeyHashFromRow(\$row, 0);\n /** @var \$obj {$objectClassName} */\n if (null === (\$obj = {$tableMapClassName}::getInstanceFromPool(\$key))) {"; if ($this->table->getChildrenColumn()) { $script .= "\n // class must be set each time from the record row\n \$cls = {$tableMapClassName}::getOMClass(\$row, 0);\n \$cls = substr('.'.\$cls, strrpos('.'.\$cls, '.') + 1);\n " . $this->builder->buildObjectInstanceCreationCode('$obj', '$cls') . "\n \$obj->hydrate(\$row);\n {$tableMapClassName}::addInstanceToPool(\$obj, \$key);"; } else { $script .= "\n " . $this->builder->buildObjectInstanceCreationCode('$obj', '$cls') . "\n \$obj->hydrate(\$row);\n {$tableMapClassName}::addInstanceToPool(\$obj, \$key);"; } $script .= "\n }\n\n // compute level\n // Algorithm shamelessly stolen from sfPropelActAsNestedSetBehaviorPlugin\n // Probably authored by Tristan Rivoallan\n if (\$level === null) {\n \$level = 0;\n \$i = 0;\n \$prev = array(\$obj->getRightValue());\n } else {\n while (\$obj->getRightValue() > \$prev[\$i]) {\n \$i--;\n }\n \$level = ++\$i;\n \$prev[\$i] = \$obj->getRightValue();\n }\n\n // update level in node if necessary\n if (\$obj->getLevel() !== \$level) {\n \$obj->setLevel(\$level);\n \$obj->save(\$con);\n }\n }\n \$dataFetcher->close();\n}\n"; }
public function objectAttributes(ObjectBuilder $builder) { $tableName = $this->table->getName(); $objectClassName = $builder->getObjectClassName(); $script = "\n/**\n * Queries to be executed in the save transaction\n * @var array\n */\nprotected \$nestedSetQueries = array();\n\n/**\n * Internal cache for children nodes\n * @var null|ObjectCollection\n */\nprotected \$collNestedSetChildren = null;\n\n/**\n * Internal cache for parent node\n * @var null|{$objectClassName}\n */\nprotected \$aNestedSetParent = null;\n\n/**\n * Left column for the set\n */\nconst LEFT_COL = '" . $tableName . '.' . $this->behavior->getColumnConstant('left_column') . "';\n\n/**\n * Right column for the set\n */\nconst RIGHT_COL = '" . $tableName . '.' . $this->behavior->getColumnConstant('right_column') . "';\n\n/**\n * Level column for the set\n */\nconst LEVEL_COL = '" . $tableName . '.' . $this->behavior->getColumnConstant('level_column') . "';\n"; if ($this->behavior->useScope()) { $script .= "\n/**\n * Scope column for the set\n */\nconst SCOPE_COL = '" . $tableName . '.' . $this->behavior->getColumnConstant('scope_column') . "';\n"; } return $script; }