コード例 #1
0
    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 addUpdateLoadedNodes(&$script)
 {
     $queryClassName = $this->queryClassName;
     $objectClassName = $this->objectClassName;
     $tableMapClassName = $this->tableMapClassName;
     $script .= "\n/**\n * Reload all already loaded nodes to sync them with updated db\n *\n * @param      {$objectClassName} \$prune        Object to prune from the update\n * @param      ConnectionInterface \$con        Connection to use.\n */\nstatic public function updateLoadedNodes(\$prune = null, ConnectionInterface \$con = null)\n{\n    if (Propel::isInstancePoolingEnabled()) {\n        \$keys = array();\n        /** @var \$obj {$objectClassName} */\n        foreach ({$tableMapClassName}::\$instances as \$obj) {\n            if (!\$prune || !\$prune->equals(\$obj)) {\n                \$keys[] = \$obj->getPrimaryKey();\n            }\n        }\n\n        if (!empty(\$keys)) {\n            // We don't need to alter the object instance pool; we're just modifying these ones\n            // already in the pool.\n            \$criteria = new Criteria({$tableMapClassName}::DATABASE_NAME);";
     if (1 === count($this->table->getPrimaryKey())) {
         $pkey = $this->table->getPrimaryKey();
         $col = array_shift($pkey);
         $script .= "\n            \$criteria->add(" . $this->builder->getColumnConstant($col) . ", \$keys, Criteria::IN);";
     } else {
         $fields = array();
         foreach ($this->table->getPrimaryKey() as $k => $col) {
             $fields[] = $this->builder->getColumnConstant($col);
         }
         $script .= "\n\n            // Loop on each instances in pool\n            foreach (\$keys as \$values) {\n              // Create initial Criterion\n                \$cton = \$criteria->getNewCriterion(" . $fields[0] . ", \$values[0]);";
         unset($fields[0]);
         foreach ($fields as $k => $col) {
             $script .= "\n\n                // Create next criterion\n                \$nextcton = \$criteria->getNewCriterion(" . $col . ", \$values[{$k}]);\n                // And merge it with the first\n                \$cton->addAnd(\$nextcton);";
         }
         $script .= "\n\n                // Add final Criterion to Criteria\n                \$criteria->addOr(\$cton);\n            }";
     }
     $script .= "\n            \$dataFetcher = {$queryClassName}::create(null, \$criteria)->setFormatter(ModelCriteria::FORMAT_STATEMENT)->find(\$con);\n            while (\$row = \$dataFetcher->fetch()) {\n                \$key = {$tableMapClassName}::getPrimaryKeyHashFromRow(\$row, 0);\n                /** @var \$object {$objectClassName} */\n                if (null !== (\$object = {$tableMapClassName}::getInstanceFromPool(\$key))) {";
     $n = 0;
     foreach ($this->table->getColumns() as $col) {
         if ($col->isLazyLoad()) {
             continue;
         }
         if ($col->getPhpName() == $this->getColumnPhpName('left_column')) {
             $script .= "\n                    \$object->setLeftValue(\$row[{$n}]);";
         } elseif ($col->getPhpName() == $this->getColumnPhpName('right_column')) {
             $script .= "\n                    \$object->setRightValue(\$row[{$n}]);";
         } elseif ($this->getParameter('use_scope') == 'true' && $col->getPhpName() == $this->getColumnPhpName('scope_column')) {
             $script .= "\n                    \$object->setScopeValue(\$row[{$n}]);";
         } elseif ($col->getPhpName() == $this->getColumnPhpName('level_column')) {
             $script .= "\n                    \$object->setLevel(\$row[{$n}]);\n                    \$object->clearNestedSetChildren();";
         }
         $n++;
     }
     $script .= "\n                }\n            }\n            \$dataFetcher->close();\n        }\n    }\n}\n";
 }