public function preSelectQuery(QueryBuilder $builder)
    {
        $columns = [];
        foreach ($this->getParameters() as $parameter) {
            $parameter = explode(' ', $parameter);
            $column = $this->getTable()->getColumn($parameter[0]);
            $columnConstant = $builder->getColumnConstant($column);
            $direction = isset($parameter[1]) ? strtoupper($parameter[1]) : Criteria::ASC;
            switch ($direction) {
                case Criteria::ASC:
                    $columns[$columnConstant] = 'Ascending';
                    break;
                case Criteria::DESC:
                    $columns[$columnConstant] = 'Descending';
                    break;
                default:
                    throw new InvalidArgumentException('DefaultOrderBehavior only accepts "asc" or "desc" as direction parameter');
            }
        }
        if (empty($columns)) {
            throw new InvalidArgumentException('DefaultOrderBehavior needs at least one column parameter');
        }
        $script = 'if (!$this->getOrderByColumns()) {
    $this';
        $prefix = '';
        if (count($columns) > 1) {
            $prefix = "\n        ";
        }
        foreach ($columns as $column => $direction) {
            $script .= $prefix . "->add{$direction}OrderByColumn({$column})";
        }
        $script .= ';
}';
        return $script;
    }
 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";
 }
예제 #3
0
 public function queryMethods(QueryBuilder $builder)
 {
     $script = '';
     foreach ($this->delegates as $delegate => $type) {
         $delegateTable = $this->getDelegateTable($delegate);
         foreach ($delegateTable->getColumns() as $column) {
             if (!$this->isColumnForeignKeyOrDuplicated($column)) {
                 $phpName = $column->getPhpName();
                 $fieldName = $column->getName();
                 $tablePhpName = $delegateTable->getPhpName();
                 $childClassName = 'Child' . $builder->getUnprefixedClassName();
                 $script .= $this->renderTemplate('queryMethodsTemplate', compact('tablePhpName', 'phpName', 'childClassName', 'fieldName'));
             }
         }
     }
     if ($this->delegates) {
         $script .= "\n/**\n * Adds a condition on a column based on a column phpName and a value\n * Uses introspection to translate the column phpName into a fully qualified name\n * Warning: recognizes only the phpNames of the main Model (not joined tables)\n * <code>\n * \$c->filterBy('Title', 'foo');\n * </code>\n *\n * @see Criteria::add()\n *\n * @param string \$column     A string representing thecolumn phpName, e.g. 'AuthorId'\n * @param mixed  \$value      A value for the condition\n * @param string \$comparison What to use for the column comparison, defaults to Criteria::EQUAL\n *\n * @return \$this|ModelCriteria The current object, for fluid interface\n */\npublic function filterBy(\$column, \$value, \$comparison = Criteria::EQUAL)\n{\n    if (isset(\$this->delegatedFields[\$column])) {\n        \$methodUse = \"use{\$this->delegatedFields[\$column]}Query\";\n\n        return \$this->{\$methodUse}()->filterBy(\$column, \$value, \$comparison)->endUse();\n    } else {\n        return \$this->add(\$this->getRealColumnName(\$column), \$value, \$comparison);\n    }\n}\n";
     }
     return $script;
 }