1

我在 Symfony2 项目中使用 Gedmo 树。

$em = $this->getDoctrine()->getEntityManager();

$options = array(
    'decorate' => true,
    'rootOpen' => '<ul>',
    'rootClose' => '</ul>',
    'childOpen' => '<li>',
    'childClose' => '</li>',
    'nodeDecorator' => function($node) {
        return '<a class="menu" href="#">'.$node['name'].'</a>';
    }
);


$category = $em->getRepository('MyMainBundle:Category')->childrenHierarchy(
    null, /* starting from root nodes */
    false, /* load all children, not only direct */
    $options
);

print_R($category);

它有效,但问题是字段名称是可翻译的。在类别表中它是 NULL 并且值保存在 category_translations 表中,但我的代码仅返回类别表中的字段。

尝试添加侦听器:

$evm = new \Doctrine\Common\EventManager();

$translatableListener = new \Gedmo\Translatable\TranslatableListener();
$translatableListener->setTranslatableLocale('en');
$evm->addEventSubscriber($translatableListener);

并改变:

$em = $this->getDoctrine()->getEntityManager();

和:

$em = $this->getDoctrine()->getEntityManager(null,null,$evm);

但没有帮助。

阅读查询提示,但没有使用它的查询。

4

1 回答 1

1

这应该可行: 创建一个自定义 NestedTreeRepository并覆盖getNodesHierarchyQuery方法以在返回的查询上调用setHint

namespace My\Bundle\Entity;

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

/**
 * CategoryRepository
 */
class CategoryRepository extends NestedTreeRepository
{
    /**
     * {@inheritDoc}
     */
    public function getNodesHierarchyQuery($node = null, $direct = false, array $options = array(), $includeNode = false)
    {
        $query = $this->getNodesHierarchyQueryBuilder($node, $direct, $options, $includeNode)->getQuery();
        $query->setHint(
            \Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER,
            'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker'
        );
        return $query;
    }
}

不要忘记将您的存储库分配给您的实体:

namespace My\Bundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;

/**
 * Category
 *
 * @Gedmo\Tree(type="nested")
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="CategoryRepository")
 */
class Category
{
}
于 2013-04-06T13:29:17.323 回答