Gedmo Tree使用getRepository() - > find()否返回Gedmo对象

时间:2015-02-13 15:07:31

标签: symfony

use Doctrine\ORM\Query;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Doctrine\ORM\EntityRepository;
use Gedmo\Tree\Entity\Repository\NestedTreeRepository;


class SomeController extends Controller
{

    public  function getaction()
    {
        $options = array(
                'decorate' => true,
                'rootOpen' => '<ul>',
                'rootClose' => '</ul>',
                'childOpen' => '<li>',
                'childClose' => '</li>',
                'childSort' => array('fp_id' => 'asc'),
                'nodeDecorator' => function($data) use ($input) {
                return $this->renderView('Bundle.html.twig', array(
                    'post' => $post,
                    'input' => $data));
                }
            );


        return $doctrine->getRepository('Bundle:EntityClass')
            ->childrenHierarchy(
                    null,
                    false,
                    $options);

        $doctrine->getRepository('Bundle:EntityClass')->find(159990012);

    }

情况:

$doctrine->getRepository('IntranetForumBundle:I2FpPosts')

返回:

    Gedmo\Tree\Entity\Repository\NestedTreeRepository

可是:

$doctrine->getRepository('IntranetForumBundle:I2FpPosts')->find(id);

不返回Gedmo类,只返回Doctrine。

我认为在启动根节点时存在问题:

        return $doctrine->getRepository('Bundle:EntityClass')
            ->childrenHierarchy(
                    // ? starting root node,
                    false,
                    $options);

1 个答案:

答案 0 :(得分:1)

这是解决方案。使用Doctrine集成的buildTree:

<?php

use Symfony\Component\Config\Definition\Builder\TreeBuilder;

use Doctrine\ORM\EntityRepository;
use Gedmo\Tree\Entity\Repository\NestedTreeRepository;

use Doctrine\DBAL\Connection;
use Doctrine\ORM\EntityManager;


class SomeController extends Controller
{    

    public function build_parent_child_in_one_table_nested()
    {
    $doctrine = $this->container->get('doctrine');

    $options = array( ... );

    $repository = $doctrine->getRepository('Bundle:EntityClass');

    // HERE IS SOLUTION
    $query = $repository
        ->createQueryBuilder('some_builder')
        ->select('main, main2')
        ->from('Bundle:EntityClass', 'main')
        ->join('main.fpThread', 'main2')
        ->where('main.fpThread = :var')
        ->setParameter(':var', $findby_some_id) // Here find by...
        ->getQuery();

    // And finally instead childrenHierarchy use buildTree to build nested-tree elements
    return $repository->buildTree($query->getArrayResult(), $options);
    }
}