I'm using "knp Doctrine Translatable" to translate Entities. So far is working great. Now I come to a point where I would like to have a generic solution, that works for any amount of languages. So I though about using an embeded form (Collections) that will handle the Translatables for an Entity. Now all is working as should, except that for adding new translation the translatable_id is not being set. Anyone has tried to achieve this too ? I was just wondering if there is an easier way to do it, to avoid over complicating things.
So far, so good, here go my Types so you can understand better the architecture.
// Main type that has a linkTranslationType with the translations
class linkType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', 'text', array(
'label' => 'Name'
))
->add('translations', 'collection', array(
'type' => new linkTranslationType(),
'label' => false,
'allow_add' => true,
'allow_delete' => true
));
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'MyBundle\Entity\Link'
));
}
}
This is the LinkTranslationType that is rendered as "a row" per language: en_EN Anchor http//url/en
class linkTranslationType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('locale', 'text',array(
'label' => 'Anchor'
))
->add('linkText', 'text',array(
'label' => 'Anchor'
))
->add('linkUrl', 'text', array(
'label' => 'Url'
))
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'MyBundle\Entity\LinkTranslation'
));
}
}
So as an example, trying to add this new entry: en_EN Anchor http//url/en
I'm getting :
id translatable_id linkText linkUrl locale
7 NULL Anchor http//url/en en_EN
I've tried to discover how translatable_id works, but still didn't had time to examine the whole source. Lastly, I tried to setTranslatableId too, without better luck. (Update: in comments)
So far I could :
- #1 Insert a new link , but not the translations (They are saved with NULL as traslatable_id)
- #2 Save existing translations link works perfect
Some other notes to add some context:
1 I tried:
$link = new Link();
if ($form->isValid() ) {
$link->mergeNewTranslations(); // but this also does assigm the Id to the translations
}
2 To save existing translation I just passed the existing Link entity to the form builder
3 I know that I could loop and assign the Translatable elements the parent entity
But that is a hack that I'm not willing to do if I have a better option:
// persist($link); and flush()
foreach ($link->getTranslations() as $linkTranslation) {
$linkTranslation->setTranslatable($link);
$em->persist($linkTranslation);
}
$em->flush();
So of course this is not the type of answer I'm looking for :)