doumi5223 2012-02-02 14:49 采纳率: 100%
浏览 61

Symfony2 - 如何在实体字段或树枝布局中实现嵌套记录和递归函数?

I have a serious doubt about doing a combo box with nested records from an entity in Symfony2. I have read about nested tree extension for Doctrine 2 in http://gediminasm.org/article/tree-nestedset-behavior-extension-for-doctrine-2, it appears to be interesting but it does not refer how to implementing this nested tree into an entity field in a form.

Also, I have read more about recursive functions in PHP, and I have found an interesting blog where it is analyzed, here is the link http://www.sitepoint.com/hierarchical-data-database/, it explains specifically about this recursive function:

function display_children($parent, $level) {

    // Retrieve all children of $parent
    $result = mysql_query('SELECT title FROM tree WHERE parent="'.$parent.'"');

    // Display each child
    while ($row = mysql_fetch_array($result)) { 

        // Indent and display the title of this child
        echo str_repeat('  ',$level).$row['title']."
";

        // Call this function again to display this child's children
        display_children($row['title'], $level+1);
    }
}

Somebody knows how to translate this code into Symfony2 and where it would be stored (Controller, Entity, etc.). If someone has other ideas about working nested records with Twig Extensions, It would be appreciate too.

Thanks a lot your help.

  • 写回答

2条回答 默认 最新

  • dongluan2612 2012-02-02 15:35
    关注

    You can have a look to this tree implementation that is not based on nested sets but on materialzed paths: https://github.com/KnpLabs/materialized-path .

    You could imagine use its API to get a flat resultset of the tree, like in your code snippet:

    $root = $repo->find($id);
    $repo->buildTree($root);
    
    $flatArray = $root->toFlatArray(function(NodeInterface $node) {
        $pre = $node->getLevel() > 1 ? implode('', array_fill(0, $node->getLevel(), '--')) : '';
        return $pre.(string)$node;
    });
    
    return $this->get('templating')->render('::tree.html.twig', $flatArray);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?