Goal:
I have a category entity. Every category can have subcategories.
Now I want to get specific category nodes by id with all its parents
My current solution (custom repository function):
public function getChildrenHierarchyByCategoryIDs($categoryIDsArray)
{
$qb = $this->getNodesHierarchyQueryBuilder();
$qb->andWhere('node.id in (:categoryIDs)')
->setParameter('categoryIDs', $categoryIDsArray)
->orWhere('node.id in (:rootNodes)') // "Workaround", get all available root nodes (1st level only) manually, because I don't know how to get them by children's parentID (node.parent) "automatically" (with second level etc.)
->setParameter('rootNodes', $this->getRootNodes())
;
$aComponents = $qb->getQuery()->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
return $this->buildTreeArray($aComponents);
}
Question:
How can I get the parents (of all nodes found by id) automatically, instead of fetching every possible parent manually by id (See "Workaround" in code)?
I can't find any solution..
I think it should be something like: ->orWhere('node.id in (node.parent)') which is not working. ([Syntax Error] line 0, col 89: Error: Expected Literal, got 'node')
Thanks in advance for taking your time!
If you need to get all parent nodes (in the hierarchy) of given node, it's rather simple:
after this, the
$parentsarray (and the metho calling result itself) will contain all parent nodes (objects) of the given node