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

时间:2012-10-17 13:46:51

标签: symfony doctrine-orm translation

简而言之

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

  

没有为'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

然后我在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:
    # ... #

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

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

    public function setTranslatableLocale($locale)
    {
        $this->locale = $locale;
    }
    /* ... */
}

运行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: yml会显示我的所有实体以及与翻译无关的任何内容1}}然后再次运行命令,我得到以下列出的内容:

type: annotation

此时,我可以更新我的架构,并且我有一个新的[OK] Gedmo\Translatable\Entity\MappedSuperclass\AbstractTranslation [OK] Gedmo\Translatable\Entity\Translation [OK] Gedmo\Translatable\Entity\MappedSuperclass\AbstractPersonalTranslation 表。但是,在使用我的实体时,没有任何东西被插入其中,大概是因为它现在期望通过注释而不是YAML进行配置,将我的配置更改回ext_translations会再次开始抛出异常,如预期的那样。

1 个答案:

答案 0 :(得分: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;
    }
    /* ... */
}

执行此操作后,将创建附加表,并在持久保存实体时将其插入到其中。

相关问题