使用YAML使用Symfony2配置可翻译的Doctrine2扩展

Kri*_*ris 8 translation symfony doctrine-orm

简而言之

我正在编写一个Symfony2/Doctrine2应用程序,并使用YAML安装并配置了由StofDoctrineExtensionsBundle提供的可翻译扩展,但是在尝试使用具有可翻译属性的实体时,不会生成其他转换表并抛出以下异常:

没有为'Gedmo\Translatable\Entity\Translation'类找到名为'/var/www/my-project/vendor/gedmo/doctrine-extensions/lib/Gedmo/Translatable/Entity/Translation.orm.yml'的映射文件.

更详细

我正在尝试使用由StofDoctrineExtensionsBundle提供的Symfony2/Doctrine2应用程序中的Translatable扩展,但是我能找到的大多数可用文档主要针对配置的注释使用,但是我要使用YAML,因为那是我如何配置其他一切.

我的配置

我已将以下内容添加到我的composer.json文件中并运行了composer update命令:"stof/doctrine-extensions-bundle": "dev-master"并且捆绑包已在我的app/AppKernel.php文件中注册.

我的app/config/config.yml文件具有以下配置:

doctrine:
  orm:
    auto_generate_proxy_classes: %kernel.debug%
      auto_mapping: true        
      mappings:
        gedmo_translatable:
          type: yml
          prefix: Gedmo\Translatable\Entity
          dir: "%kernel.root_dir%/../vendor/gedmo/doctrine-extensions/lib/Gedmo/Translatable/Entity"
          alias: GedmoTranslatable
          is_bundle: false

stof_doctrine_extensions:
  default_locale: en_GB
  translation_fallback: true
  orm:
    default:
      timestampable: true
      translatable: true
Run Code Online (Sandbox Code Playgroud)

然后我在YAML中定义了一个实体:

Foo\ContentBundle\Entity\Article:
  type: entity
  repositoryClass: Foo\ContentBundle\Repository\ArticleRepository
  table: article
  gedmo:
    translation:
      locale: locale
  id:
    id:
      type: integer
      generator: { strategy: AUTO }
  fields:
    name:
      type: string
      length: 64
      gedmo:
        - translatable
    content:
      type: text
      gedmo:
        - translatable
    # ... #
  oneToMany:
    # ... #
Run Code Online (Sandbox Code Playgroud)

然后我运行了console命令php app/console doctrine:generate:entities FooContentBundle来生成实体类,并手动添加了locale属性和setter:

class Article
{
    /* ... */
    private $locale;

    public function setTranslatableLocale($locale)
    {
        $this->locale = $locale;
    }
    /* ... */
}
Run Code Online (Sandbox Code Playgroud)

运行后php app/console doctrine:schema:update --force,我的文章表及其关联创建,但没有任何与翻译有关(我假设一个表应该为此创建...)

然后,当使用可翻译的实体时,我得到了例外:

没有为'Gedmo\Translatable\Entity\Translation'类找到名为'/var/www/my-project/vendor/gedmo/doctrine-extensions/lib/Gedmo/Translatable/Entity/Translation.orm.yml'的映射文件.

异常引用的YAML文件在它正在寻找它的路径中不存在,我也无法在其他任何地方找到它.

有没有人对我出错的地方有任何想法?

更新:经过进一步调查......

运行php app/console doctrine:mapping:info显示我的所有实体,没有任何与翻译有关的内容,但是,如果我更新文件的gedmo_translatable:一部分app/config/config.yml并更改type: ymltype: annotation再次运行命令,我会列出以下内容:

[OK]   Gedmo\Translatable\Entity\MappedSuperclass\AbstractTranslation
[OK]   Gedmo\Translatable\Entity\Translation
[OK]   Gedmo\Translatable\Entity\MappedSuperclass\AbstractPersonalTranslation
Run Code Online (Sandbox Code Playgroud)

此时,我可以更新我的架构,并且我有一个新ext_translations表.但是,在使用我的实体时,没有任何内容被插入其中,大概是因为它现在期望通过注释而不是YAML进行配置,将我的配置更改为type: yml开始再次抛出异常,如预期的那样.

Kri*_*ris 5

在尝试了文档建议不起作用的事情之后,即在同一个包中混合注释和YAML配置,看起来我有事情可行.整个事情感觉像一个错误或不完整的实现,但我可能做错了什么.这是什么工作......

设置以下内容app/config/config.yml:doctrine.orm.mappings.gedmo_translatable.type: annotation

如我原始问题中所述,在我的YAML架构定义中设置可翻译配置,以及在我的类文件中作为注释:

/* ... */
use Gedmo\Mapping\Annotation as Gedmo;
/* ... */
class Article
{
    /* ... */

    /**
     * @Gedmo\Translatable
     * @var string $name
     */
    private $name;

    /**
     * @Gedmo\Locale
     */
    private $locale;

    public function setTranslatableLocale($locale)
    {
        $this->locale = $locale;
    }
    /* ... */
}
Run Code Online (Sandbox Code Playgroud)

执行此操作后,将创建附加表,并在持久化实体时将转换插入其中.