How to get some child nodes by id with all parents (DoctrineExtensions, Gedmo-Tree NestedTreeRepository, Symfony)

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(DoctrineORMQuery::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!

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *