I use KnpDoctrineBehaviors (Sluggable,Translatable, etc ). Field that I use for slug, I have only in MyClassTranslations. So when I add Sluggable for my translations class, I have for each i18n entry different slug. How to solve this? Thank you!
Asked
Active
Viewed 1,265 times
0
-
you want one slug for every entity or different slugs for different translations? – Nicolai Fröhlich Jul 23 '13 at 15:41
-
@nifr I want one slug for every entity. – denys281 Jul 23 '13 at 15:43
-
then just add the slug property to your base `Entity` class ... not to your `EntityTranslation` class. – Nicolai Fröhlich Jul 23 '13 at 15:44
-
@nifr yes, but in my Entity I have no field title for example, I have it in my EntityTranslation. For slug I need add method to entity `getSluggableFields`. – denys281 Jul 23 '13 at 20:00
-
@smoreno I don't remember. I think that in those case I just used id instead of slug. Maybe in a two years they have some solution for this case. – denys281 May 04 '15 at 12:05
1 Answers
1
You can override the getter for title in your entity, and just call the translate method from KnpDoctrineBehaviors on your entity. It will try to get the title translation in your current locale, or fallback to default locale translation.
<?php
namespace AppBundle\Entity;
use Knp\DoctrineBehaviors\Model as ORMBehaviors;
use AppBundle\Entity\AppTrait\TranslatableEntity;
class SomeEntity
{
use ORMBehaviors\Translatable\Translatable;
use ORMBehaviors\Sluggable\Sluggable;
public function getSluggableFields()
{
return ['title'];
}
public function getTitle()
{
return $this->translate(null,true)->getTitle();
}
This implies that the slug might be updated often if you update the title with different locale, you might want to avoid this, and just use english title for slug generation:
public function getTitle()
{
return $this->translate('en')->getTitle();
}

Jean-Christophe Meillaud
- 1,961
- 1
- 21
- 27