Author: david Date: Thu Feb 10 17:45:04 2011 New Revision: 8959 Log: Add nestedset:build task.
Added: trunk/lib/task/nestedsetBuildTask.class.php (contents, props changed) Added: trunk/lib/task/nestedsetBuildTask.class.php ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ trunk/lib/task/nestedsetBuildTask.class.php Thu Feb 10 17:45:04 2011 (r8959) @@ -0,0 +1,106 @@ +<?php + +/* + * This file is part of Qubit Toolkit. + * + * Qubit Toolkit is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Qubit Toolkit is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Qubit Toolkit. If not, see <http://www.gnu.org/licenses/>. + */ + +/** + * Regenerate nested set column values + * + * @package symfony + * @subpackage task + * @author David Juhasz <[email protected]> + * @version SVN: $Id$ + */ +class nestedsetBuiltTask extends sfBaseTask +{ + /** + * @see sfTask + */ + protected function configure() + { + $this->addArguments(array( + )); + + $this->addOptions(array( + new sfCommandOption('application', null, sfCommandOption::PARAMETER_OPTIONAL, 'The application name', true), + new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'cli'), + new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'propel'), + )); + + $this->namespace = 'nestedset'; + $this->name = 'build'; + $this->briefDescription = 'Build all nested set values.'; + + $this->detailedDescription = <<<EOF +Build all nested set values. +EOF; + } + + /** + * @see sfTask + */ + public function execute($arguments = array(), $options = array()) + { + $this->logSection('nestedset', 'Build nested set values...'); + + $sql = 'SELECT id, parent_id'; + $sql .= ' FROM '.QubitInformationObject::TABLE_NAME; + $sql .= ' ORDER BY parent_id ASC, lft ASC'; + + $databaseManager = new sfDatabaseManager($this->configuration); + + $conn = $databaseManager->getDatabase('propel')->getConnection(); + + $sth = $conn->query($sql); + $results = $sth->fetchAll(PDO::FETCH_ASSOC); + + $tree = array(); + foreach ($results as $row) + { + if ($row['parent_id'] !== null) + { + if (isset($tree[$row['parent_id']])) + { + array_push($tree[$row['parent_id']]['children'], $row['id']); + } + else + { + $tree[$row['parent_id']]['children'] = array($row['id']); + } + } + } + + self::get_width($tree[1]); + + print_r($tree[1]['width']); + } + + protected function get_width(&$node) + { + foreach ($node['children'] as $childId) + { + if (isset($tree[$childId])) + { + $node['width'] = self::get_width($tree[$childId]) + 1; + } + else + { + return 2; + } + } + } +} -- You received this message because you are subscribed to the Google Groups "Qubit Toolkit Commits" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/qubit-commits?hl=en.
