0

I have variable $path which contains the array of the names of sibling categories. Using this variable I would like to create a new array $categories_children, that would contain the array of children of each category from $path array according to its title. I'm using Doctrine Tree-Nestedset extension by Gediminas Morkevicius and I've tried this:

    $em = $this->getDoctrine()->getManager();
    $repo = $em->getRepository('MyProjectAdminBundle:Category');

    $category = $repo->findOneById($id);       
    $path = $repo->getPath($category);

    $categories_children = array();

    foreach($path as $node){
        $parent = $repo->findOneByTitle($node);
        $categories_children[] = $repo->getChildren($parent, true);
    }

The problem is that method getChildren() with argument $parent returns the same array as like when the passed argument is NULL. That means it will return all nodes starting with root, instead of starting with selected category. Somehow method findOneByTitle(), which is being used in $parent variable, doesn't accept arguments from getPath() and behaves as NULL.

Ladislav M
  • 2,157
  • 4
  • 36
  • 52

1 Answers1

0

getChildren $parent argument specify only from which root element get the tree. You can see code of childrenQueryBuilder function in NestedTreeRepository.

To fetch all childs I use own function in repository.

public function getTreeAsFlatArray( AbstractTreeNode $parent = null, array $options = array() ) {
    $qb = $this->createQueryBuilder('node');    
    $qb
        ->andWhere('node.lft > :parent_lft')
            ->setParameter('parent_lft', $parent->getLft() )
        ->andWhere('node.lft < :parent_rgt')
            ->setParameter('parent_rgt', $parent->getRgt() )
        ->andWhere('node.level <= :level')
            ->setParameter('level', $parent->getLevel() + $o['depth'])
        ->andWhere('node.root = :root')
            ->setParameter('root', $parent->getRoot())
    ;

    ...
}   

If you only need direct childs simplets way to specify childs field in entity

/**
 * @ORM\OneToMany(targetEntity="AbstractTreeNode", mappedBy="parent", fetch="EXTRA_LAZY")
 * @ORM\OrderBy({"lft" = "ASC"})
 */
protected $childs;
Alexey B.
  • 11,965
  • 2
  • 49
  • 73
  • Now when I use getTreeAsFlatArray($parent), it returns "FatalErrorException: Error: Call to a member function getLft() on a non-object". – Ladislav M Jul 04 '13 at 15:31
  • its just an example query how you can fetch childs of node, node ready to use function. – Alexey B. Jul 07 '13 at 17:16
  • Ok, i will adjust getTreeAsFlatArray() just for my usage. I also tried to use childs field with get method in entity. To fetch childs I simply used $categories_children[] = $repo->getChilds(). But the results ware still the same: "Error: Call to a member function getChilds() on a non-object." I don't understand, how can method findOneByTitle() return something else than object. Thanks. – Ladislav M Jul 07 '13 at 19:02
  • it can be `null` if there is not search result – Alexey B. Jul 08 '13 at 08:08
  • There are valid results for search, that can be return as array of strings. In entity I also find method called __toString(), which simply returns title of category. Couldn't this method influence the way how the whole entity works? – Ladislav M Jul 11 '13 at 06:56
  • if you need only titles add `$qb->select('node.title')` – Alexey B. Jul 11 '13 at 07:00
  • do you know by any chance how I should modify it to have the parent in the array while doing this : $arrayTree = $repo->childrenHierarchy(); – Eagle1 Feb 07 '14 at 20:56