使用条令1.2和Symfony 1.4获取节点祖先


Getting node ancestors using Doctrine 1.2 and Symfony 1.4

我在尝试从一个节点获取所有祖先时遇到了一点麻烦;

这是我的计划。yml:

Constante:
connection: doctrine
tableName: constante
actAs:
NestedSet:
  hasManyRoots: true
  rootColumnName: parent_id
columns:
id:
  type: integer(8)
  fixed: false
  unsigned: false
  primary: true
  autoincrement: true
parent_id:
  type: integer(8)
  fixed: false
  unsigned: false
  primary: false
  notnull: true
  autoincrement: false
lft:
  type: integer(8)
  fixed: false
  unsigned: false
  primary: false
  notnull: true
  autoincrement: false
rgt:
  type: integer(8)
  fixed: false
  unsigned: false
  primary: false
  notnull: true
  autoincrement: false
level:
  type: integer(8)
  fixed: false
  unsigned: false
  primary: false
  notnull: true
  autoincrement: false
cod_interno:
  type: string(5)
  fixed: false
  unsigned: false
  primary: false
  notnull: false
  autoincrement: false
nombre:
  type: string(64)
  fixed: false
  unsigned: false
  primary: false
  notnull: true
  autoincrement: false

这就是我试图从一个节点(不是根)获得所有祖先的方法

$path        = Doctrine_Core::getTable('Constante')->find($condicion); // $condicion = 57
$node        = $path->getNode();
$isLeaf      = $node->isLeaf(); //var_dump throws true
$ancestors   = $node->getAncestors(); //var_dump throws false
$isValidNode = $node->isValidNode(); //var_dump throws true

作为$ancestors == false,我无法对其进行迭代并获得所有祖先(我正在尝试构建一个简单的面包屑)

这是我在数据库中存储的,这是真实的数据(仅用于测试小狗)

+---------++---------++---------++---------++----------++---------+
|ID       ||PARENT_ID||LFT      ||RGT      ||LEVEL     ||NOMBRE   |
|---------||---------||---------||---------||----------||---------|
|56       ||56       ||1        ||4        ||0         ||COUNTRY  | --> this is root
|57       ||56       ||2        ||3        ||1         ||CANADA   | --> child of root
+---------++---------++---------++---------++----------++---------+

据此,如果Ancestors返回false,则表示所选节点是根节点。

我花了几个小时寻找解决方案,但没有成功。

如果您需要进一步的信息,请随时询问!

编辑:我在键入表中的内容时犯了一个错误,多亏了olivierw提醒我这一点。

表中的rgt字段似乎有错误。如果id 56是根,则它应该具有rgt = 4,并且id 57应该具有rgt = 3。所以你的表格应该是:

+---------++---------++---------++---------++----------++---------+
|ID       ||PARENT_ID||LFT      ||RGT      ||LEVEL     ||NOMBRE   |
|---------||---------||---------||---------||----------||---------|
|56       ||56       ||1        ||4        ||0         ||COUNTRY  |
|57       ||56       ||2        ||3        ||1         ||CANADA   |
+---------++---------++---------++---------++----------++---------+

所以你会得到正确的祖先。

我想分享我的解决方案,希望它对其他人有用:

行动:

//action.class.php
public function executeIndex(sfWebRequest $request) {
$condicion = $request->getParameter('id') ? $request->getParameter('id') : 0;
if ($condicion > 0) {
  $path = $tree = Doctrine_Core::getTable('Constante')->find($condicion);
} else {
  $tree = Doctrine_Core::getTable('Constante');
  $path = null;
}
$this->constantes = $tree;
$this->path = $path;
$this->setTemplate('index');
}

视图:

//indexSuccess.php
<?php
$var = (is_null($sf_request->getParameter('id'))) ? $constantes->getTree()->fetchRoots() : $constantes->getNode()->getChildren();
?>
<?php if ($var): foreach ($var as $constante): ?>
    <tr>
      <td><?php echo $constante->getNombre() ?></td>
    </tr>
  <?php
  endforeach;
endif;
?>

部分:

//_breadcrumb.php
<?php
if ($path) {
  echo '<ul class="breadcrumb">';
  $node = $path->getNode();
  $ancestors = $node->getAncestors();
  if ($ancestors)
    foreach ($ancestors AS $ancestor) {
      echo '<li>' . link_to($ancestor->getNombre(), 'constantes/more?id=' . $ancestor->getId()) . '<span class="divider">/</span></li>';
    }
  echo '</ul>';
}
?>

我认为代码是不言自明的,但如果你有任何问题,请告诉我!