首页 文章

Sylius:使用可翻译内容添加资源

提问于
浏览
3

我正在构建一个基于Sylius标准版的应用程序 . 使用ResourceBundle我处理以集成我自己的实体和相应的关系 . 这些新资源应该稍后与产品实体相关,但首先我想让它“独立”运行 . 后端适用于增加的资源和关系 . 这些可以通过表单集合进行编辑 . 很好!现在我想为我的新资源获取翻译的数据库内容 . 我尝试过我之前在Symfony中做过的方式,但它没有用 . 最后几天我尝试了所有可能的解决方案,但没有一个工作,或者我犯了错误...在打字时构造的翻译表:

app/console doctrine:schema:update --force

在表格中也看不到可翻译的内容 . 调用编辑操作时,我收到以下错误:

error:
Neither the property "translations" nor one of the methods "getTranslations()", "translations()", "isTranslations()", "hasTranslations()", "__get()" exist and have public access in class "KontaktBundle\Entity\Kontakte".

有人带有可转换数据库内容的扩展Sylius资源的示例实现吗?我还在学习symfony和sylius,你能告诉我我错过了什么或做错了吗?

非常感谢@gvf . 现在我发现必须设置配置条目 . 这给了我一个功能性的例子,我想在这里提供:

配置

# app/config/sylius_config.yml (must be imported in config)

# Adding Resource
sylius_resource:
    # Resource Settings
    settings:
        sortable: true
        paginate: 50
        allowed_paginate: [50, 100, 500]
        filterable: true

    resources:
        dbk.authors:
            driver: doctrine/orm
            templates: AuthorBundle:Backend
            object_manager: default
            classes:
                model: AuthorBundle\Entity\Kontakte
                #interface: // if you have an interface configured
                controller: Sylius\Bundle\ResourceBundle\Controller\ResourceController
                repository: Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository
                translation:
                    model: AuthorBundle\Entity\AuthorsTranslation
                    mapping:
                        fields: ['anrede','biografie']

服务

# app/config/sylius_services.yml (must be imported in config)
parameters:
    # Parameter for our author entity
    app.form.type.authors.class: AuthorBundle\Form\AuthorsType
    app.form.type.authors_translation.class: AuthorBundle\Form\AuthorsTranslationType

services:
    # Adding Authors Backend menu Item
    dbk_backend_authors.menu_builder:
        class: AuthorBundle\EventListener\MenuBuilderListener
        tags:
            - { name: kernel.event_listener, event: sylius.menu_builder.backend.main, method: addBackendMenuItems }
            - { name: kernel.event_listener, event: sylius.menu_builder.backend.sidebar, method: addBackendMenuItems }

    # Adding Authors FormType
    app.form.type.authors:
        class: "%app.form.type.authors.class%"
        tags: 
            - {name: form.type, alias: dbk_authors }

    app.form.type.authors_translation:
        class: "%app.form.type.authors_translation.class%"
        tags: 
            - {name: form.type, alias: dbk_authors_translation }

EventListener(在Sylius-Backend中添加菜单项)

<?php

// AuthorBundle/EventListener/MenuBuilderListener/MenuBuilderListener.php

namespace AuthorBundle\EventListener;

use Sylius\Bundle\WebBundle\Event\MenuBuilderEvent;

class MenuBuilderListener
{
    public function addBackendMenuItems(MenuBuilderEvent $event)
    {
        $menu = $event->getMenu();

        $menu['assortment']->addChild('vendor', array(
            'route' => 'Authors',
            'labelAttributes' => array('icon' => 'glyphicon glyphicon-user'),
        ))->setLabel('Authors');
    }
}

实体

作者(我们要添加和翻译的新资源)

<?php

// AuthorBundle/Entity/Authors.php

namespace AuthorBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Sylius\Component\Translation\Model\AbstractTranslatable;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * Authors
 *
 * @ORM\Entity
 * @ORM\Table(name="authors")
 */
class Authors extends AbstractTranslatable
{

    //
    // IDENTIFIER FIELDS
    //

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="bigint", length=20, nullable=false, options={"unsigned":"true"})
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;


    //
    // FIELDS
    //

    /**
     * @var string
     *
     * @ORM\Column(name="vorname", type="string", length=255)
     */
    private $vorname;

    /**
     * @var string
     *
     * @ORM\Column(name="nachname", type="string", length=255)
     */
    private $nachname;

    public function __construct() {
        parent::__construct();
    }

    //
    // TranslationFields - Getters and Setters
    //

    /**
     * Get Anrede
     * @return string
     */
    public function getAnrede()
    {
        return $this->translate()->getAnrede();
    }

    /**
     * Set Anrede
     *
     * @param string $anrede
     *
     * @return Authors
     */
    public function setAnrede($anrede)
    {
        $this->translate()->setAnrede($anrede);
        return $this;
    }

    /**
     * Get Biografie
     * @return string
     */
    public function getBiografie()
    {
        return $this->translate()->getBiografie();
    }

    /**
     * Set Biografie
     *
     * @param string $biografie
     *
     * @return Authors
     */
    public function setBiografie($biografie)
    {
        $this->translate()->setBiografie($biografie);
        return $this;
    }


    //
    // Getters and Setters
    //

    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }


    /**
     * Set vorname
     *
     * @param string $vorname
     *
     * @return Authors
     */
    public function setVorname($vorname)
    {
        $this->vorname = $vorname;

        return $this;
    }

    /**
     * Get vorname
     *
     * @return string
     */
    public function getVorname()
    {
        return $this->vorname;
    }

    /**
     * Set nachname
     *
     * @param string $nachname
     *
     * @return Authors
     */
    public function setNachname($nachname)
    {
        $this->nachname = $nachname;

        return $this;
    }

    /**
     * Get nachname
     *
     * @return string
     */
    public function getNachname()
    {
        return $this->nachname;
    }


    public function  __toString(){
        return $this->getFullName();
    }

}

AuthorsTranslation(这是作者翻译的实体)

<?php

// AuthorBundle/Entity/AuthorsTranslation.php

namespace AuthorBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Sylius\Component\Translation\Model\AbstractTranslation;

/**
 * AuthorsTranslation
 *
 * @ORM\Entity
 * @ORM\Table(name="authors_translation")
 */
class AuthorsTranslation extends AbstractTranslation
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="bigint", length=20, nullable=false, options={"unsigned":"true"})
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    //
    // TRANSLATABLE - FIELDS
    //

    /**
     * @var string
     * @ORM\Column(name="anrede", type="string", length=255)
     */
    private $anrede;

    /**
     * @var string
     * @ORM\Column(name="biografie", type="text")
     */
    private $biografie;

    /**
     * {@inheritdoc}
     */
    public function getId()
    {
        return $this->id;
    }

    //
    // GETTERS AND SETTERS
    //

    /**
     * Set anrede
     *
     * @param string $anrede
     * @return Authors
     */
    public function setAnrede($anrede)
    {
        $this->anrede = $anrede;
        return $this;
    }

    /**
     * Get anrede
     *
     * @return string
     */
    public function getAnrede()
    {
        return $this->anrede;
    }


    /**
     * Set biografie
     *
     * @param string $biografie
     *
     * @return Authors
     */
    public function setBiografie($biografie)
    {
        $this->biografie = $biografie;
        return $this;
    }

    /**
     * Get biografie
     *
     * @return string
     */
    public function getBiografie()
    {
        return $this->biografie;
    }



}

FormTypes

AuthorsType

<?php

// AuthorBundle/Form/AuthorsType.php

namespace AuthorBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Doctrine\ORM\QueryBuilder;

class AuthorsType extends AbstractType
{

     /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            // Add Translations to Form.
            ->add('translations', 'a2lix_translations', array(
                'required' => false,
                'fields' => array(
                    'anrede' => array(
                        'label' => 'Anrede',
                    ),
                    'biografie' => array(
                        'label' => 'Biografie',
                        'attr' => array('data-edit' => 'wysiwyg', 'rows' => '15'),
                        'required' => false,
                    )
                )
            ))

            ->add('vorname', null, array(
                'label' => 'Vorname',
                'required' => false,
            ))

            ->add('nachname', null, array(
                'label' => 'Nachname',
                'required' => false,
            ))
        ;
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
            $resolver->setDefaults(array(
                'csrf_protection'   => false,
                'data_class' => 'AuthorBundle\Entity\Authors'
            ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'dbk_authors';
    }
}

AuthorsTranslationType

<?php

// AuthorBundle/Form/AuthorsTranslationType.php

namespace AuthorBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class AuthorsTranslationType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('anrede', 'text', array(
                'label' => 'Anrede'
            ))
            ->add('biografie', 'textarea', array(
                'label' => 'Biografie'
            ))
        ;
    }

    /**
     * {@inheritdoc}
     */
    public function getName()
    {
        return 'dbk_authors_translation';
    }
}

表格模板

{# AuthorBundle/Resources/views/backend/edit||add.html.twig - as you like... the call of form.translation (line 5) is the point here #}
{% form_theme form 'SyliusWebBundle:Backend:forms.html.twig' %}
<div class="row">
    <div class="col-md-12">
        {{ form_row(form.translations, {'attr': {'class': 'input-lg'}}) }}
        {{ form_row(form.vorname) }}
        {{ form_row(form.nachname) }}
    </div>
</div>

1 回答

  • 2

    查看Sylius Components中models列表下的Product和 ProductTranslation ,查看Sylius如何实现它的示例 .

    Kontakte 需要扩展 AbstractTranslatable ,还需要创建一个扩展 AbstractTranslation 的类 KontakteTranslation . 在 sylius_resource 下,您还需要配置翻译:

    sylius_resource:
    resources:
        dbk.contact:
            driver: doctrine/orm
            templates: KontaktBundle:Backend
            object_manager: default
            classes:
                model: KontaktBundle\Entity\Kontakte
                controller: Sylius\Bundle\ResourceBundle\Controller\ResourceController
                repository: Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository
                translation:
                    model: KontaktBundle\Entity\KontakteTranslation
                    mapping:
                        fields: {...fields that will be translated...}
    

    摆脱gedmo可翻译扩展,因为Sylius不使用它 .

相关问题