【问题标题】:Getting node ancestors using Doctrine 1.2 and Symfony 1.4使用 Doctrine 1.2 和 Symfony 1.4 获取节点祖先
【发布时间】:2012-07-06 23:37:21
【问题描述】:

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

这是我的 schema.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
+---------++---------++---------++---------++----------++---------+

According to this如果Ancestors返回false,则表示选中的节点是根节点。

我花了几个小时寻找一个没有运气的解决方案。

如果您需要更多信息,请不要犹豫!

编辑:我在输入表格中的内容时出错了,感谢 olivierw 提醒我这一点。

【问题讨论】:

    标签: php symfony-1.4 nodes doctrine-1.2 nested-sets


    【解决方案1】:

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

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

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

    【讨论】:

    • 您好,我已经发布了我的解决方案,请查看!非常感谢
    【解决方案2】:

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

    行动:

    //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>';
    }
    ?>
    

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

    【讨论】:

    • 太棒了!但我不明白为什么$node-&gt;getAncestors() 现在正在工作并且不要像您的问题那样返回false
    • 嗨!我从头开始重写了这个函数,我认为上一个函数中的问题是一个 setBaseQuery 的语句不正确。我认为这个很优雅,最好
    • setBaseQuery ?嗬嗬!这太优雅了!
    猜你喜欢
    • 2011-11-10
    • 1970-01-01
    • 1970-01-01
    • 2012-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多