Doctrine 2 self-referenced entity without unnecessary queries

◇◆丶佛笑我妖孽 提交于 2019-12-05 17:16:47
Florian

If you know the depth of your tree, you can just do a custom dql query and do:

return $this->createQueryBuilder('example')
    ->addSelect('children')
    ->leftJoin('example.children', 'children')
    ->addSelect('subChildren')
    ->leftJoin('children.children', 'subChildren')
;

Otherwise, and as stated here, you can generate a flat resultset, and then construct the tree from it.

I made this kind of implementation using materialized paths, but nothing forbids you to do it with foreign keys comparison:

https://github.com/KnpLabs/DoctrineBehaviors/blob/master/src/Knp/DoctrineBehaviors/ORM/Tree/Tree.php#L119

https://github.com/KnpLabs/DoctrineBehaviors/blob/master/src/Knp/DoctrineBehaviors/Model/Tree/Node.php#L219

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!