Symfony gedmo树+可翻译

时间:2016-12-18 09:25:39

标签: php symfony doctrine-orm tree

在配置gedmo库时出现问题后,我已经解决了切换到stofdoctrineextensionbundle的问题。

这是我的实体:http://pastebin.com/8nXrLsTh

我正在遵循官方文档对各种学说扩展进行一些测试。

编辑配置文件:

config.yml:
orm:
        auto_generate_proxy_classes: "%kernel.debug%"
        naming_strategy: doctrine.orm.naming_strategy.underscore
        auto_mapping: true

        mappings:
            gedmo_translatable:
                type: annotation
                prefix: Gedmo\Translatable\Entity
                dir: "%kernel.root_dir%/../vendor/gedmo/doctrine-extensions/lib/Gedmo/Translatable/Entity"
                alias: GedmoTranslatable # (optional) it will default to the name set for the mapping
                is_bundle: false
            gedmo_translator:
                type: annotation
                prefix: Gedmo\Translator\Entity
                dir: "%kernel.root_dir%/../vendor/gedmo/doctrine-extensions/lib/Gedmo/Translator/Entity"
                alias: GedmoTranslator # (optional) it will default to the name set for the mapping
                is_bundle: false
            gedmo_loggable:
                type: annotation
                prefix: Gedmo\Loggable\Entity
                dir: "%kernel.root_dir%/../vendor/gedmo/doctrine-extensions/lib/Gedmo/Loggable/Entity"
                alias: GedmoLoggable # (optional) it will default to the name set for the mappingmapping
                is_bundle: false
            gedmo_tree:
                type: annotation
                prefix: Gedmo\Tree\Entity
                dir: "%kernel.root_dir%/../vendor/gedmo/doctrine-extensions/lib/Gedmo/Tree/Entity"
                alias: GedmoTree # (optional) it will default to the name set for the mapping
                is_bundle: false

现在我创建一个测试菜单树,只是为了理解它的功能:

/**
 * @Route("/createtestmenu")
 */
public function createtestmenuAction() {
    $food = new Menu();
    $food->setTitle('cereali');
    $food->setTranslatableLocale('it_IT');

    $fruits = new Menu();
    $fruits->setTitle('grano');
    $fruits->setParent($food);
    $fruits->setTranslatableLocale('it_IT');

    $vegetables = new Menu();
    $vegetables->setTitle('farro');
    $vegetables->setParent($food);
    $vegetables->setTranslatableLocale('it_IT');

    $carrots = new Menu();
    $carrots->setTitle('avena');
    $carrots->setParent($vegetables);
    $carrots->setTranslatableLocale('it_IT');

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

    $em->persist($food);
    $em->persist($fruits);
    $em->persist($vegetables);
    $em->persist($carrots);
    $em->flush();

    return new Response('creato menu');
}

在此之后我创建了树可翻译的翻译。我已经翻译了除1节点之外的几乎所有内容来理解可翻译的后备。

/**
 * @Route("/traducimenu")
 */
public function traducimenuAction() {
    $em = $this->getDoctrine()->getManager();

    $cereali = $em->find('AppBundle:Menu', 13);
    $cereali->setTitle("Cereals");
    $cereali->setTranslatableLocale('en_GB'); // change locale
    $em->persist($cereali);
    $em->flush();

    $grano = $em->find('AppBundle:Menu', 14);
    $grano->setTitle("wheat");
    $grano->setTranslatableLocale('en_GB'); // change locale
    $em->persist($grano);
    $em->flush();

    $farro = $em->find('AppBundle:Menu', 15);
    $farro->setTitle("spelt");
    $farro->setTranslatableLocale('en_GB'); // change locale
    $em->persist($farro);
    $em->flush();

    /* $farro = $em->find('AppBundle:Menu', 11);
       $farro->setTitle("spelt");
       $farro->setTranslatableLocale('en_GB'); // change locale
       $em->persist($farro);
       $em->flush(); */

    return new Response('traduzione creata');
}

现在我使用基本语言生成树,但是我无法检索已翻译的树,实际上我只能检索一个节点。

/**
 * @Route("/menu")
 */
public function readmenuAction() {

$em = $this->getDoctrine()->getRepository('AppBundle:Menu');
$options = array(
    'decorate' => true,
    'rootOpen' => '<ul>',
    'rootClose' => '</ul>',
    'childOpen' => '<li>',
    'childClose' => '</li>',
    /*'nodeDecorator' => function($node) {
        return '<a href="/page/' . $node['slug'] . '">' . $node[$field].'</a>';
    }*/
);

$em2 = $this->getDoctrine()->getManager();
$node = $em2->find('AppBundle:Menu', 13);
$node->setTranslatableLocale('en_GB');
$em2->refresh($node);

/**
 * translation dump
 */
dump($node);

$htmlTree = $em->childrenHierarchy(
        $node, /* starting from root nodes */
        false, /* false: load all children, true: only direct */
        $options,
        true
);

dump($htmlTree);
return $this->render('AppBundle:menu:menutest.html.twig', array(
            'menutest' => $htmlTree,
                )
);

}

如果我得到一个单一节点,我就可以获得基本语言和翻译,但不幸的是我不明白如何获得完整的翻译树。我该怎么做??不幸的是官方文档非常差,如果有人可以给我一些输入来开始学习...谢谢你。

0 个答案:

没有答案