2

I've been working the past 2 hours on translating my slug without much success. First, let's take a look at my entity :

/**
 * BlogPost
 *
 * @ORM\Entity
 */
class BlogPost implements Translatable
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @Gedmo\Translatable
     * @ORM\Column(name="title", type="string", length=128)
     */
    private $title;

    /**
     * @Gedmo\Slug(fields={"title"})
     * @Gedmo\Translatable
     * @ORM\Column(length=128)
     */
    private $slug;

It's pretty straightforward. Now, when I do :

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

$blogPost = new BlogPost();
$blogPost->setTitle('my title in FRANCAIS');
$blogPost->setTranslatableLocale('fr_ca');
$em->persist($blogPost);
$em->flush();

$blogPost->setTitle('my title in ENGLISH');
$blogPost->setTranslatableLocale('en_us');
$em->persist($blogPost);
$em->flush();

Only my title gets translated, but my slug is only in french. I tried that solution from the doc ... but there is no TranslationListener (the file does not exist). There is only one note in the doc that doesn't mean much to me :

Note: these translations will not be processed as ordinary fields of your object, in case if you translate a slug additional translation will not know how to generate the slug, so the value as an additional translation should be processed when creating it.

I must admit that I feel in a dead end. Could someone please share some knowledge on that matter!

Michael Villeneuve
  • 3,933
  • 4
  • 26
  • 40

1 Answers1

1

I suggest you to use the KnpDoctrineBehavious Bundle. It makes is very simple to translate any property of your entity:

class BlogPost
{
    use ORMBehaviors\Translatable\Translation;

    // anything that should not be translated
    // follows in this class
}

And add a translation entity:

class BlogPostTranslation
{
    /**
     * @Gedmo\Slug(fields={"title"})
     * @ORM\Column(length=128)
     */
    private $slug;

    // ...
}

Now you can acces any translation like this:

$blogPost->getSlug(); // default language slug
$blogPost->translate('en')->getSlug(); // English slug
$blogPost->translate('fr')->getSlug(); // French slug

Don't forget to call $entity->mergeNewTranslations(); after $em->persist($entity); to update the translation table.

Edit:

Note that the DoctrineBehaviours Bundle also supports a way better Sluggable. Use it like it's shown in their documentation.

ferdynator
  • 6,245
  • 3
  • 27
  • 56
  • 1
    I will take a look at it and come back to you tomorrow ! Thanks for taking the time to help. – Michael Villeneuve Apr 02 '14 at 20:02
  • Alright so I've tried using DoctrineBehaviours Bundle and it's indeed, pretty nice. However, I can't get the translate behaviour to work! I keep getting a weird error. It's looking for an Entity that doesn't exist. The message is : target-entity MyProject\MyBundle\Enti cannot be found in MyProject\MyBundle\Entity\BlogPost#translatable. Any idea? – Michael Villeneuve Apr 04 '14 at 15:03
  • Sounds like a typo to me. Did you register the [Listeners](https://github.com/KnpLabs/DoctrineBehaviors#listeners)? – ferdynator Apr 04 '14 at 15:44
  • Yes listeners is registered. Most of the code is straight copy/paste from doc. Hmm – Michael Villeneuve Apr 04 '14 at 15:47
  • Just to provide more information, I simply imported the folder (copy/paste) in config.yml. I didn't do the doctrine2 way. – Michael Villeneuve Apr 04 '14 at 15:48
  • That is the correct way. Does the error message really say **MyProject\MyBundle\Enti cannot be found**? – ferdynator Apr 04 '14 at 15:50
  • Yes, using the log I went in doctrine and var_dump() the data. The "TargetEntity" is really looking for a folder \Enti. I tried using sluggable and it works perfectly. It's only when I try to translate it that the bug happen. I'm thinking about submiting a ticket on github. – Michael Villeneuve Apr 04 '14 at 15:52
  • Did you add the `@ORM\Entity` annotation to your translation entity? Is the database schema up-to-date? Could you add the new entities to your original question or create a new one? – ferdynator Apr 04 '14 at 15:56
  • 1
    Ok, I will close this one as this is for another subject. Thanks for the help I will post in the comment the link to the new question. – Michael Villeneuve Apr 04 '14 at 16:00
  • http://stackoverflow.com/questions/22867913/translation-behaviour-is-looking-for-enti-folder – Michael Villeneuve Apr 04 '14 at 16:16