Trying to translate entities :(

1,239 views
Skip to first unread message

ori...@gmail.com

unread,
Aug 20, 2013, 1:26:13 AM8/20/13
to symfony2adm...@googlegroups.com
Hi, i will become crazy trying to use A2lixTranslation with AdminGenerator to translate my entities.
I will give you a lot of code to you have all the elements in your hand to be able to help me.
Im using Gedmo/translatable and is working ok, i make tests and if you create a entity is a controller and create some translations, Gedmo/translatable make the correct changes and insertion to BD.

im using symfony 2.3 and in composer.json i have this:
    ...
    "cedriclombardot/admingenerator-user-bundle": "dev-master",
    "cedriclombardot/admingenerator-generator-bundle": "2.3.*@dev",
    "a2lix/translation-form-bundle": "dev-master",
    ...

I have a simple entity StaticText, and i want to translate one field. This is the code:

    /**
     * besmart\CoreBundle\Entity\StaticText
     *
     * @ORM\Entity
     * @ORM\Table(name="static_text")
     * @Gedmo\TranslationEntity(class="besmart\CoreBundle\Entity\Translation\StaticTextTranslation")
     * @ORM\Entity(repositoryClass="besmart\CoreBundle\Repository\StaticTextRepository")
     */
    class StaticText
    {
        ...
       
        /**
         * @var text $description
         * @Gedmo\Translatable
         * @ORM\Column(name="description", type="text", nullable=true)
         */
        protected $description;
       
        /**
         * @Gedmo\Locale
         * Used locale to override Translation listener`s locale
         * this is not a mapped field of entity metadata, just a simple property
         */
        private $locale;   
       
        /**
         * @ORM\OneToMany(targetEntity="besmart\CoreBundle\Entity\Translation\StaticTextTranslation",mappedBy="object", cascade={"persist", "remove"})
         */   
        private $translations;

       
        public function getTranslations()
        {
            return $this->translations;
        }

        public function addTranslation(Translation\StaticTextTranslation $t)
        {
            if (!$this->translations->contains($t)) {
                $this->translations[] = $t;
                $t->setObject($this);
            }
        }
       
        public function setTranslatableLocale($locale)
        {
            $this->locale = $locale;
        }
       
        ...
    }
   
The StaticTextTranslation entity:
    /**
     * @ORM\Entity
     * @ORM\Table(name="static_text_translations",
     *     uniqueConstraints={@ORM\UniqueConstraint(name="lookup_unique_idx", columns={
     *         "locale", "object_id", "field"
     *     })}
     * )
     */
    class StaticTextTranslation extends AbstractPersonalTranslation
    {
        /**
         * Convinient constructor
         *
         * @param string $locale
         * @param string $field
         * @param string $value
         */
        public function __construct($locale, $field, $value)
        {
            $this->setLocale($locale);
            $this->setField($field);
            $this->setContent($value);
        }

        /**
         * @ORM\ManyToOne(targetEntity="besmart\CoreBundle\Entity\StaticText", inversedBy="translations")
         * @ORM\JoinColumn(name="object_id", referencedColumnName="id", onDelete="CASCADE")
         */
        protected $object;
    }
   
This is my A2lixTranslationForm configuration in config.yml

    a2lix_translation_form:
        locales: [en]       # [optional] Array of the translation locales (The default locale have to be excluded). Can also be specified in the form builder.
        default_required: false     # [optional] Defaults to false. In this case, translation fields are not mark as required with HTML5.
        use_aop: true               # [optional] Defaults to false.

And the admingenerator condiguration(in config.yml):

    admingenerator_generator:
        use_doctrine_orm: true   
    #    thumbnail_generator:  avalanche
        dashboard_welcome_path: dashboard_admin
        login_path: ~
        logout_path: fos_user_security_logout
        base_admin_template:  AdmingeneratorGeneratorBundle::base_admin_assetic_less.html.twig
        twig:
            use_form_resources: true
            use_localized_date: false
            date_format: Y-m-d
            datetime_format: Y-m-d H:i:s
            localized_date_format: medium
            localized_datetime_format: medium
            number_format:
                decimal: 0
                decimal_point: .
                thousand_separator: ,
               
               
               
               
               
In the StaticText-generator.yml i try this:
    ...
    fields:
      translations:
           formType: a2lix_translations

    and got this error(when try to access to "/new" route for StaticText) :
        FatalErrorException: Error: Call to undefined method besmart\CoreBundle\Entity\StaticText::getTranslationEntityClass() in D:\docs\WORK\besmart\site\vendor\a2lix\translation-form-bundle\A2lix\TranslationFormBundle\TranslationForm\DefaultTranslationForm.php line 15

    and then try this:   
    fields:
      translations:
           formType: a2lix_translations_gedmo

    and got this error(when try to access to "/new" route for StaticText):
        "Class does not exist"

Please, any help will be very appreciate, im not obliged to use gedmo\Translatable, if there is a better solution i adopt it.
Even better is a example of how to configure a complete use case, that´s something very important in the documentations.
Thanks in advance.

ori...@gmail.com

unread,
Aug 20, 2013, 2:47:36 AM8/20/13
to symfony2adm...@googlegroups.com, ori...@gmail.com
now i make another change in StaticText-generator.yml

i try this:
   
    ... 
    fields:
        translations:
            formType: a2lix_translations_gedmo
            addFormOptions:
              translatable_class: "besmart\CoreBundle\Entity\StaticText"

and the new url (/admin/StaticText/new) is visible with the translation taps, but when i submit the form i got this error:

    Warning: Missing argument 1 for besmart\CoreBundle\Entity\Translation\StaticTextTranslation::__construct(), called in D:\docs\WORK\besmart\site\vendor\a2lix\translation-form-bundle\A2lix\TranslationFormBundle\Form\DataMapper\GedmoTranslationMapper.php on line 76 and defined in D:\docs\WORK\besmart\site\src\besmart\CoreBundle\Entity\Translation\StaticTextTranslation.php line 25


the line 25 in StaticTextTranslation in in the constructor:

        public function __construct($locale, $field, $value)
        {
            $this->setLocale($locale);
            $this->setField($field);
            $this->setContent($value);
        }

thanks



net...@gmail.com

unread,
Aug 20, 2013, 5:58:03 AM8/20/13
to symfony2adm...@googlegroups.com, ori...@gmail.com
Have you tried without the __constructor() in your personal translation class? Something like this:

    /**
     * @ORM\Entity
     * @ORM\Table(name="static_text_translations",
     *     uniqueConstraints={@ORM\UniqueConstraint(name="lookup_unique_idx", columns={
     *         "locale", "object_id", "field"
     *     })}
     * )
     */
    class StaticTextTranslation extends AbstractPersonalTranslation
    {
        
/** 
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;

/** 
* @ORM\Column(type="string", length=2, nullable=true)
*/
protected $locale;

/** 
* @ORM\Column(type="string", length=255, nullable=true)
*/
protected $field;

/** 
* @ORM\Column(type="text", nullable=true)
*/
protected $content;

ori...@gmail.com

unread,
Aug 20, 2013, 11:51:17 AM8/20/13
to symfony2adm...@googlegroups.com, ori...@gmail.com
Thank
Your proposal is very close to the solution, i just change the constructor to a empty one, but all the properties ins the class are not needed, remember is inherit from AbstractPersonalTranslation and there is those properties. The class are now like this:

namespace besmart\CoreBundle\Entity\Translation;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Translatable\Entity\MappedSuperclass\AbstractPersonalTranslation;


/**
 * @ORM\Entity
 * @ORM\Table(name="static_text_translations",
 *     uniqueConstraints={@ORM\UniqueConstraint(name="lookup_unique_idx", columns={
 *         "locale", "object_id", "field"
 *     })}
 * )
 */
class StaticTextTranslation extends AbstractPersonalTranslation
{
    /**
     * Convinient constructor
     */
    public function __construct()
    {
Reply all
Reply to author
Forward
0 new messages