Наконец-то выкладываю нужные куски кода для заточки деревьем под Propel 1.2. Тестировалось на Symfony 1.1.6.
Для того чтобы заработали деревья необходимо воспользоваться плагином sfPropelActAsNestedSetBehaviorPlugin.
Ваш _list.php будет выглядеть примерно так:
<tbody>
<?php foreach ($pager->getResults() as $i => $tree): $odd = fmod(++$i, 2) ? 'odd' : 'even' ?>
<tr id="node-<?php echo $tree->getId(); ?>" class="sf_admin_row <?php echo $odd ?><?php
// insert hierarchical info
if ($tree->getTreeParent())
{
echo " child-of-node-".$tree->getTreeParent();
}
?>">
<?php include_partial('list_td_batch_actions', array('tree' => $tree)) ?>
<?php include_partial('list_td_tabular', array('tree' => $tree)) ?>
<?php include_partial('list_td_actions', array('tree' => $tree)) ?>
</tr>
<?php endforeach; ?>
</tbody>
Так же изменится executeBatchOrder в модуле. Выглядеть он будет так:
public function executeBatchOrder(sfWebRequest $request)
{
$newparent = $request->getParameter('newparent');
$ids = array();
foreach ($newparent as $key => $val) {
$ids[$key] = true;
if (!empty($val))
$ids[$val] = true;
}
$ids = array_keys($ids);
//validate if all id's exist
$validator = new sfValidatorPropelChoiceMany(array('model' => 'Tree'));
try {
// validate ids
$ids = $validator->clean($ids);
// the id's validate, now update the tree
$count = 0;
$flash = "";
foreach ($newparent as $id => $parentId) {
if (!empty($parentId)) {
$node = TreePeer::retrieveByPK($id);
$parent = TreePeer::retrieveByPK($parentId);
/**
* Данное условие задаёт различное поведение.
* Если элементы одного и того же уровня - тот они
* рассматриваются как соседи.
* Если же уровни разные и один не является родителем другого - то
* как parent->child
*
*/
if (!$parent->isDescendantOf($node) && $parent->getLevel() != $node->getLevel()) {
$node->moveToFirstChildOf($parent);
$node->save();
$count++;
$flash .= "<br/>Moved '".$node->getTitle()."' under '".$parent->getTitle()."'.";
} else {
$node->moveToNextSiblingOf($parent);
$node->save();
$count++;
$flash .= "<br/>Moved '".$node->getTitle()."' after '".$parent->getTitle()."'.";
}
}
}
if ($count > 0) {
$this->getUser()->setFlash('notice', sprintf("Tree order updated, moved %s item%s:".$flash, $count, ($count > 1 ? 's' : '')));
} else {
$this->getUser()->setFlash('error', "You must at least move one item to update the tree order");
}
}
catch (sfValidatorError $e) {
$this->getUser()->setFlash('error', 'Cannot update the tree order, maybe some item are deleted, try again');
}
$this->redirect('tree/list');
}
Вобщем-то это все отличия. Всё остальное ничем не отличается.