relez
relez

Reputation: 790

Problems trying to translate an entity Symfony2

I am trying to set some translatable contents in Symfony2 with Gedmo Translatable but seems I am doing something wrong or missing something.

I included the line in the composer.json file:

"gedmo/doctrine-extensions": "2.3.*@dev"

And also, I added this lines in the config.yml file:

stof_doctrine_extensions:
    orm:
        alopatria:
            timestampable: true
            sluggable: true
            translatable: true

The entity class is set like this:

<?php

namespace ...;

use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Translatable\Translatable;

/**
* ...\Entity
*
* @ORM\Table(name="content")
* @ORM\HasLifecycleCallbacks 
* @ORM\Entity(repositoryClass="...\Entity\ContentRepository")
*/
class Content implements Translatable
{
    /**
     *
     * @ORM\Column(name="id", type="bigint", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    protected $id;

    /**
     * @Gedmo\Translatable
     * @ORM\Column(name="title", type="string", length=32, nullable=false)
     */
    protected $title;

    /**
     * @Gedmo\Translatable
     * @ORM\Column(name="text", type="text", nullable=false)
     */
    protected $text;

    /**
     * @var datetime $created
     *
     * @Gedmo\Timestampable(on="create")
     * @ORM\Column(type="datetime")
     */
    private $created;

    /**
     * @var datetime $updated
     *
     * @Gedmo\Timestampable(on="update")
     * @ORM\Column(type="datetime")
     */
    private $updated;

    /**
     * @var datetime $contentChanged
     *
     * @ORM\Column(name="content_changed", type="datetime", nullable=true)
     * @Gedmo\Timestampable(on="change", field={"title", "text"})
     */
    private $contentChanged;

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


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set title
     *
     * @param string $title
     * @return Content
     */
    public function setTitle($title)
    {
        $this->title = $title;

        return $this;
    }

    /**
     * Get title
     *
     * @return string 
     */
    public function getTitle()
    {
        return $this->title;
    }

    /**
     * Set text
     *
     * @param string $text
     * @return Content
     */
    public function setText($text)
    {
        $this->text = $text;

        return $this;
    }

    /**
     * Get text
     *
     * @return string 
     */
    public function getText()
    {
        return $this->text;
    }

    /**
     * Set created
     *
     * @param \DateTime $created
     * @return Content
     */
    public function setCreated($created)
    {
        $this->created = $created;

        return $this;
    }

    /**
     * Get created
     *
     * @return \DateTime 
     */
    public function getCreated()
    {
        return $this->created;
    }

    /**
     * Set updated
     *
     * @param \DateTime $updated
     * @return Content
     */
    public function setUpdated($updated)
    {
        $this->updated = $updated;

        return $this;
    }

    /**
     * Get updated
     *
     * @return \DateTime 
     */
    public function getUpdated()
    {
        return $this->updated;
    }

    /**
     * Set contentChanged
     *
     * @param \DateTime $contentChanged
     * @return Content
     */
    public function setContentChanged($contentChanged)
    {
        $this->contentChanged = $contentChanged;

        return $this;
    }

    /**
     * Get contentChanged
     *
     * @return \DateTime 
     */
    public function getContentChanged()
    {
        return $this->contentChanged;
    }

    /**
     * Set slug
     *
     * @param string $slug
     * @return Content
     */
    public function setSlug($slug)
    {
        $this->slug = $slug;

        return $this;
    }

    /**
     * Get slug
     *
     * @return string 
     */
    public function getSlug()
    {
        return $this->slug;
    }
}

When I try to create a translatable content in my controller:

$content = new Content();
$content->setTitle('Content example');
$content->setText('Content example...');
$em = $this->getDoctrine()->getEntityManager();
$em->persist($content);
$em->flush();

$content->setTranslatableLocale('fr'); // change locale
$em->persist($content);
$em->flush();

This is the error:

The class 'Gedmo\Translatable\Entity\Translation' was not found in the chain configured namespaces ...\Entity, FOS\UserBundle\Model

Any help? Thanks!!!

Upvotes: 1

Views: 1172

Answers (1)

Thomas Decaux
Thomas Decaux

Reputation: 22711

You need to configure the translatable Entity to use as well. In config.yml:

orm:
auto_generate_proxy_classes: %kernel.debug%
mappings:
    translatable:
        type: annotation
        is_bundle: false
        prefix: Gedmo\Translatable\Entity
        dir: "%kernel.root_dir%/../vendor/gedmo/doctrine-extensions/lib/Gedmo/Translatable/Entity"
        alias: GedmoTranslatable

Upvotes: 2

Related Questions