【问题标题】:Doctrine 2: Recursively select all self-referenced associated entities (NestedSet)原则 2:递归选择所有自引用的关联实体(NestedSet)
【发布时间】:2011-11-16 09:47:54
【问题描述】:

我已经定义了一个自引用实体:Category as shown in the docs

它基本上创建了一个不可排序的树。

我应该执行什么 DQL 查询来选择所有父级、父级的父级......?

编辑:我目前选择所有类别,离开加入他们的父母。然后我使用实体方法提取所有祖先,无需额外查询:

public function hereToRoot( $allCategories )
{
    $ancestors = array();
    $leftJoinedParent = $this->getParent();
    //add parents
    while ( !is_null($leftJoinedParent) ) {
        $nextAncestor = $allRoles[$leftJoinedParent->getId()];
        $ancestors[] = $nextAncestor;
        $leftJoinedParent = $nextAncestor->getParent();
    }
    return array_reverse($ancestors);
}

【问题讨论】:

    标签: tree doctrine-orm dql


    【解决方案1】:

    如果你想拥有树状实体,最好使用corresponding extensions。他们的 GitHub 存储库是 here

    【讨论】:

    • 这对我的任务来说是不必要的复杂 :)。目前手动 PHP 操作相当简单。
    猜你喜欢
    • 1970-01-01
    • 2013-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-26
    • 2018-07-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多