I use the Symfony version 3.4.0 and I try to use this bundle to translate entities : https://github.com/webfactory/WebfactoryPolyglotBundle
So I created two entities ( Film and FilmTranslation ) for the test
Here my Film.php
JavaScript
x
<?php
namespace AppBundleEntity;
use DoctrineORMMapping as ORM;
use WebfactoryBundlePolyglotBundleAnnotation as Polyglot;
use WebfactoryBundlePolyglotBundleTranslatableInterface;
use DoctrineCommonCollectionsCollection;
use DoctrineCommonCollectionsArrayCollection;
/**
* Film
*
* @ORMTable(name="film")
* @ORMEntity(repositoryClass="AppBundleRepositoryFilmRepository")
* @PolyglotLocale(primary="fr_FR")
*/
class Film
{
/**
* @var int
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
* @PolyglotTranslationCollection
*/
private $id;
/**
* @var string
*
* @ORMColumn(name="Titre", type="string", length=255, unique=true)
* @PolyglotTranslatable
* @var string|TranslatableInterface
*/
private $titre;
/**
* @ORMOneToMany(targetEntity="FilmTranslation", mappedBy="entity")
* @PolyglotTranslationCollection
* @var Collection
*/
private $translations;
public function __construct()
{
$this->translations = new ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set titre
*
* @param string $titre
*
* @return Film
*/
public function setTitre($titre)
{
$this->titre = $titre;
return $this;
}
/**
* Get titre
*
* @return string
*/
public function getTitre()
{
return $this->titre;
}
/**
* Add translation
*
* @param AppBundleEntityFilmTranslation $translation
*
* @return Film
*/
public function addTranslation(AppBundleEntityFilmTranslation $translation)
{
$this->translations[] = $translation;
return $this;
}
/**
* Remove translation
*
* @param AppBundleEntityFilmTranslation $translation
*/
public function removeTranslation(AppBundleEntityFilmTranslation $translation)
{
$this->translations->removeElement($translation);
}
/**
* Get translations
*
* @return DoctrineCommonCollectionsCollection
*/
public function getTranslations()
{
return $this->translations;
}
}
And here my FilmTranslation.php
JavaScript
<?php
namespace AppBundleEntity;
use DoctrineORMMapping as ORM;
use WebfactoryBundlePolyglotBundleEntityBaseTranslation;
/**
* FilmTranslation
*
* @ORMTable(name="film_translation")
* @ORMEntity(repositoryClass="AppBundleRepositoryFilmTranslationRepository")
* @ORMTable(
* uniqueConstraints = {
* @ORMUniqueConstraint(columns={"entity_id", "locale"})
* }
* )
*/
class FilmTranslation extends BaseTranslation
{
/**
* @var string
*
* @ORMColumn(name="titre", type="string", length=255, unique=true)
*/
private $titre;
/**
* @ORMManyToOne(targetEntity="Film", inversedBy="translations")
* @var Film
*/
protected $entity;
/**
* Set titre
*
* @param string $titre
*
* @return FilmTranslation
*/
public function setTitre($titre)
{
$this->titre = $titre;
return $this;
}
/**
* Get titre
*
* @return string
*/
public function getTitre()
{
return $this->titre;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set locale
*
* @param string $locale
*
* @return FilmTranslation
*/
public function setLocale($locale)
{
$this->locale = $locale;
return $this;
}
/**
* Set entity
*
* @param AppBundleEntityFilm $entity
*
* @return FilmTranslation
*/
public function setEntity(AppBundleEntityFilm $entity = null)
{
$this->entity = $entity;
return $this;
}
/**
* Get entity
*
* @return AppBundleEntityFilm
*/
public function getEntity()
{
return $this->entity;
}
}
I’m able to create a form but when I try to persist and flush I’ve the following error :
No mapping found for field ‘id’ on class ‘AppBundleEntityFilm’.
Is something I’m doing wrong ?
Advertisement
Answer
So remove @PolyglotTranslationCollection
from $id
annotation like this 😉
JavaScript
class Film
{
/**
* @var int
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;