过滤器主义嵌套集树子层次结构 [英] filter doctrine nested set tree children hierarchy
问题描述
我可以使用
轻松检索整个树
$ repo-> childrenHierarchy(
null,/ *从根节点开始* /
false,/ * false:加载所有的孩子,直接* /
$ options
)
我想要的是过滤这个树的实体根据外键(类别属于用户,因此每个类别都有一个userId)
不幸的是,接受在该节点上过滤的回调的选项不允许对外键进行过滤,外键值不包括在节点数组中:
$ options = array(
'decorate'=> true,
'rootOpen'=>'< ul>',
'rootClose'=>'< / ul>',
'childOpen '=> function($ node)use($ user){
// $ node不包含任何外键
if($ node ['userId']!= $ user-> getId()){
return null;
}
return< li id ='$ node ['id']。'>;
},
'childClose'=> '< / li>',
);
我如何解决这个问题?
我终于找到了解决方案,您必须在实体的存储库中创建一个新的自定义方法,并使用自己的过滤器扩展原始的queryBuilder,如下所示:
public function getTree()
{
$ qb = $ this-> getNodesHierarchyQueryBuilder();
$ qb
- > andWhere('node.status =:status')
- > setParameter('status',1)
- > andWhere('node .deletedAt IS NULL')
;
$ aComponents = $ qb-> getQuery() - > getResult(\Doctrine\ORM\Query :: HYDRATE_ARRAY);
return $ this-> buildTreeArray($ aComponents);
}
最后,您调用方法buildTreeArray以创建树结构。
I am using a doctrine nested set tree in my application.
I can easily retrieve the whole tree using
$repo->childrenHierarchy(
null, /* starting from root nodes */
false, /* false: load all children, true: only direct */
$options
)
What I want though is to filter entities of this tree according to a foreign key (categories belong to users so each category has a userId)
Unfortunately the option that accepts a callback to filter on the node does not allow filtering on a foreign key, foreign keys values are not included in the node array :
$options = array(
'decorate' => true,
'rootOpen' => '<ul>',
'rootClose' => '</ul>',
'childOpen' => function ($node) use($user) {
// $node does not contain any foreign key
if ($node['userId'] != $user->getId()) {
return null;
}
return "<li id='".$node['id']."'>";
},
'childClose' => '</li>',
);
How can I solve this issue ?
I finally found the solution, you have to create a new custom method in the repository of the entity and extend the original queryBuilder with your own filters like so:
public function getTree()
{
$qb = $this->getNodesHierarchyQueryBuilder();
$qb
->andWhere('node.status = :status')
->setParameter('status', 1)
->andWhere('node.deletedAt IS NULL')
;
$aComponents = $qb->getQuery()->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
return $this->buildTreeArray($aComponents);
}
At the end, you call the method buildTreeArray in order to create the tree structure.
这篇关于过滤器主义嵌套集树子层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!