Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring the Translatable Doctrine2 extension with Symfony2 using YAML

In a nutshell

I'm writing a Symfony2 / Doctrine2 app and have installed and configured the Translatable extension provided by StofDoctrineExtensionsBundle using YAML, however no additional translation table(s) are generated and the following exception is thrown when attempting to work with entities that have translatable properties:

No mapping file found named '/var/www/my-project/vendor/gedmo/doctrine-extensions/lib/Gedmo/Translatable/Entity/Translation.orm.yml' for class 'Gedmo\Translatable\Entity\Translation'.

In more detail

I'm trying to get the Translatable extension working in my Symfony2 / Doctrine2 application that is provided by the StofDoctrineExtensionsBundle, however most of the available documentation I can find mainly targets the usage of annotations for configuration, but I'm going with YAML because that's how I have configured everything else.

My configuration

I have added the following to my composer.json file and have ran the composer update command: "stof/doctrine-extensions-bundle": "dev-master" and the bundle is registered in my app/AppKernel.php file.

My app/config/config.yml file has the following configuration:

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

I have then defined an entity in 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:
    # ... #

I have then ran the console command php app/console doctrine:generate:entities FooContentBundle to generate the entity classes, and have manually added the locale property and setter:

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

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

After running php app/console doctrine:schema:update --force, my article table is created along with its associations, but nothing relating to translations (I'm assuming a table is supposed to be created for this...)

Then, when working with an entity that is translatable, I'm getting the exception:

No mapping file found named '/var/www/my-project/vendor/gedmo/doctrine-extensions/lib/Gedmo/Translatable/Entity/Translation.orm.yml' for class 'Gedmo\Translatable\Entity\Translation'.

The YAML file that the exception is referencing does not exist in the path that it's looking for it within, neither could I find it anywhere else.

Does anyone have any ideas as to where I'm going wrong?

Update: After further investigation...

Running php app/console doctrine:mapping:info displays all of my entities and nothing relating to translations, however, if I update the gedmo_translatable: part of my app/config/config.yml file and change type: yml to type: annotation then run the command again, I get the following listed:

[OK]   Gedmo\Translatable\Entity\MappedSuperclass\AbstractTranslation
[OK]   Gedmo\Translatable\Entity\Translation
[OK]   Gedmo\Translatable\Entity\MappedSuperclass\AbstractPersonalTranslation

At which point, I can update my schema, and I have a new ext_translations table. However, nothing is being inserted into it when working with my entities, presumably because it's now expecting configuration by annotation rather than YAML, changing my config back to type: yml starts throwing the exception again, as expected.

like image 225
Kris Avatar asked Oct 17 '12 13:10

Kris


1 Answers

After trying things that the documentation suggests will not work, i.e. mixing both annotation and YAML configurations in the same bundle, it would appear I have things working. The whole thing feels like a bug or an incomplete implementation, however I may be doing something incorrectly. Here's what's working...

Setting the following in app/config/config.yml: doctrine.orm.mappings.gedmo_translatable.type: annotation

Setting the translatable configuration in my YAML schema definition as outlined in my original question, as well as as an annotation in my class file:

/* ... */
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;
    }
    /* ... */
}

After doing this, the additional table is created and translations are being inserted into it when persisting the entity.

like image 138
Kris Avatar answered Sep 29 '22 18:09

Kris