PHP code example of vm5 / entity-translations-bundle
1. Go to this page and download the library: Download vm5/entity-translations-bundle library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
vm5 / entity-translations-bundle example snippets
namespace Example;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="languages")
*/
class Language implements \Arxy\EntityTranslationsBundle\Model\Language
{
/**
* @ORM\Id
* @ORM\Column(type="integer", length=11)
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(name="locale", type="string", length=5)
*/
protected $locale;
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @param mixed $id
* @return Language
*/
public function setId($id)
{
$this->id = $id;
return $this;
}
/**
* @return mixed
*/
public function getLocale(): string
{
return $this->locale;
}
/**
* @param mixed $locale
* @return Language
*/
public function setLocale($locale)
{
$this->locale = $locale;
return $this;
}
}
namespace Example;
use Doctrine\ORM\Mapping as ORM;
use Arxy\EntityTranslationsBundle\Model\Translatable;
use Arxy\EntityTranslationsBundle\Model\Translation;
/**
* @ORM\Entity()
* @ORM\Table(name="news")
*/
class News implements Translatable
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
*/
protected $id;
/**
* @ORM\OneToMany(targetEntity="NewsTranslation", mappedBy="translatable", cascade={"ALL"}, orphanRemoval=true)
*/
protected $translations;
/**
* @var NewsTranslation
*/
private $currentTranslation;
public function getTranslations()
{
return $this->translations;
}
/**
* This is important, as form has default option: by_reference = false
* so here we set the mapped side entity.
* @param NewsTranslation|null $translation
*/
public function addTranslation(NewsTranslation $translation)
{
$this->getTranslations()->add($translation);
$translation->setTranslatable($this);
}
/**
* This is also used by form.
* @param NewsTranslation|null $translation
*/
public function removeTranslation(NewsTranslation $translation)
{
$this->getTranslations()->removeElement($translation);
}
/**
* This method is used by bundle to inject current translation.
*/
public function setCurrentTranslation(Translation $translation = null): void
{
$this->currentTranslation = $translation;
}
/**
* @return string|null
*/
public function getTitle()
{
return !$this->currentTranslation ?: $this->currentTranslation->getTitle();
}
}
namespace Example;
use Arxy\EntityTranslationsBundle\Model\Language;use Doctrine\ORM\Mapping as ORM;
use Arxy\EntityTranslationsBundle\Model\Translation;
/**
* @ORM\Entity
* @ORM\Table(name="news_translations")
*/
class NewsTranslation implements Translation
{
/**
* @var News
* @ORM\Id
* @ORM\ManyToOne(targetEntity="News", inversedBy="translations")
*/
protected $translatable;
/**
* @var Language
* @ORM\Id
* @ORM\ManyToOne(targetEntity="Language")
*/
protected $language;
/**
* @var string
* @ORM\Column(type="string")
*/
protected $title;
/**
* @return News
*/
public function getTranslatable()
{
return $this->translatable;
}
/**
* @param News $translatable
*/
public function setTranslatable(News $translatable = null)
{
$this->translatable = $translatable;
}
/**
* @return Language
*/
public function getLanguage(): Language
{
return $this->language;
}
/**
* @param Language $language
*/
public function setLanguage(Language $language)
{
$this->language = $language;
}
/**
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* @param string $title
*/
public function setTitle($title)
{
$this->title = $title;
}
}
$news = new News();
$englishTranslation = new NewsTranslation();
$englishTranslation->setLanguage($englishLanguage);
$englishTranslation->setTitle('Title on english');
$news->addTranslation($englishTranslation);
$em->persist($news);
$em->flush();
public function addTranslation(NewsTranslation $translation)
{
if (!$this->translations->contains($translation)) {
$this->translations->add($translation);
$translation->setTranslatable($this);
}
}
public function removeTranslation(NewsTranslation $translation)
{
$this->translations->removeElement($translation);
$translation->setTranslatable(null);
}
use Arxy\EntityTranslationsBundle\Model\EditableTranslation;
class NewsTranslation implements EditableTranslation
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class NewsTranslationType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add(
'title',
TextType::class,
[
' );
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefault('data_class', NewsTranslation::class); // this is important
$resolver->setDefault('constraints', [
new NotNull(
[
'groups' => ['en'], // make only english