0

I need for a project translated entities and an easy way to maintain the content. One "myentity" can have many "myentity_trans".

This is easy so far, but I need a dynamic form in a easy way without changing too much in symfony2 default behaviour.

When I create/edit a "myentity", I need a "myentity_trans" subform for every language. Is there a common way to handle all this?

My Entities as example:

myentity
- id
- status

myentity_trans
- id
- myentity_id
- language_id
- ...

language
- id
- name

EDIT 1: Here my form configuration that works on edit, if I got related entities:

$builder
    ->add('name')
    ->add('trans', 'collection', array(
          'type' => new RetailerTransType(),
          'allow_add' => true,
          'allow_delete' => true
    ));

Now I like empty forms for every possible language, so the user can easy create the translations. I tried to use the query builder, but it obviously don't work with collection type.

Daniel
  • 539
  • 4
  • 25
  • See: http://symfony.com/doc/current/cookbook/form/form_collections.html – stwe Jul 11 '13 at 10:16
  • Thats what I have found in the meantime. How can I connect this with my languages table so I get for every language an empty form? If possible without another javascript request. – Daniel Jul 11 '13 at 11:12
  • no, you need javascript to add the empty form. but you don't to a request to the server. the prototype of the empty subform is already in your html. – Emii Khaos Jul 11 '13 at 12:33
  • Its works when I edit an entity, but not on create? I tried to use the querybuilder in my formtype class with a left join to get a collection with empty entities per language, but I still have a syntax error. :( Does anyone have some expirience with it? – Daniel Jul 11 '13 at 12:52
  • The problem here is that no one knows what you've done so far. What is the error message? What are your form types? No one can help but link only tutorials without more information. Please inform yourself in the FAQ how to put the right questions. – stwe Jul 11 '13 at 13:16
  • As I said, the form does what I need on edit, but not on create. I edit the main question for more code. – Daniel Jul 11 '13 at 13:22
  • If an error message? E.g. that the language_id is unknown? – stwe Jul 11 '13 at 19:14

1 Answers1

0

You should either use Gemo\DoctrineExtensions\Translatable, which can easily be integrated with symfony2 using Stof\DoctrineExtensionsBundle

... or my tip if using PHP 5.4+ with traits available KnpLabs\DoctrineBehaviors\Translatable.

In order to integrate these nicely with your forms use a2lix\TranslationFormBundle. This bundle provides exactly what you're looking for - a collection of translations for your languages.

See my answer here for a quick insight on using DoctrineBehaviors\Translatable and the current locale proxy which i found to be a really comfortable solution.

Just create class Entity and EntityTranslation, include the proxy lines ... call $entity->getProperty()

-> current locale applied automatically. as easy as it can be :-)

Community
  • 1
  • 1
Nicolai Fröhlich
  • 51,330
  • 11
  • 126
  • 130