Skip to content

Instantly share code, notes, and snippets.

@tristanbes
Created March 19, 2012 15:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tristanbes/2116290 to your computer and use it in GitHub Desktop.
Save tristanbes/2116290 to your computer and use it in GitHub Desktop.
How to dynamically add translations to your I18N object using SonataAdminBundle & DoctrineExtensions
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="demo.book.admin" class="Demo\BookBundle\Admin\BookAdmin">
<tag name="sonata.admin" manager_type="orm" group="General" label="Books"/>
<argument/>
<argument>Demo\BookBundle\Entity\Book</argument>
<argument></argument>
</service>
<service id="demo.book.admin.translation" class="Demo\BookBundle\Admin\BookTranslationAdmin">
<tag name="sonata.admin" manager_type="orm" group="Général" label="Book translation"/>
<argument/>
<argument>Demo\BookBundle\Entity\BookTranslation</argument>
<argument></argument>
</service>
</services>
</container>
<?php
namespace Demo\BookBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* Demo\BookBundle\Entity\Book
*
* @ORM\Entity()
* @ORM\Table()
* @Gedmo\TranslationEntity(class="Demo\BookBundle\Entity\BookTranslation")
*
*/
class Book
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\generatedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string $name
* @ORM\Column(type="string",length=255, unique=true)
*/
protected $name;
/**
* @var string $description
* @Gedmo\Translatable
* @ORM\Column(type="text", nullable=true)
*/
protected $description;
/**
* @ORM\OneToMany(targetEntity="BookTranslation", mappedBy="object", cascade={"persist", "remove"})
*/
private $translations;
public function __construct ()
{
$this->translations = new ArrayCollection();
}
/**
* Get id
*
* @return integer $id
*/
public function getId ()
{
return $this->id;
}
/**
* Gets translation
*/
public function getTranslations()
{
return $this->translations;
}
public function addTranslations(BookTranslation $t)
{
$this->translations[] = $t;
$t->setObject($this);
}
public function setTranslations($translations)
{
$this->translations = $translations;
}
/**
* Set name
*
* @param string $name
*/
public function setName ($name)
{
$this->name = $name;
}
/**
* Get name
*
* @return string $name
*/
public function getName ()
{
return $this->name;
}
/**
* Set description
*
* @param text $description
*/
public function setDescription ($description)
{
$this->description = $description;
}
/**
* Get description
*
* @return text $description
*/
public function getDescription ()
{
return $this->description;
}
}
<?php
namespace Demo\BookBundle\Admin;
use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
/**
* Book Admin
*/
class BookAdmin extends Admin
{
/**
* Configure the list
*
* @param \Sonata\AdminBundle\Datagrid\ListMapper $list list
*/
protected function configureListFields(ListMapper $list)
{
$list
->addIdentifier('name', null, array('label' => 'Nom'))
->add('_action', 'actions', array(
'actions' => array(
'edit' => array()
)
));
}
/**
* Configure the form
*
* @param FormMapper $formMapper formMapper
*/
public function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('name', null, array('label' => 'Name'))
->add('translations', 'sonata_type_collection', array(), array(
'edit' => 'inline',
'inline' => 'table',
'sortable' => 'position'
));
}
}
<?php
namespace Demo\BookBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Translatable\Entity\MappedSuperclass\AbstractPersonalTranslation;
/**
* @ORM\Entity
* @ORM\Table(name="book_translations",
* uniqueConstraints={@ORM\UniqueConstraint(name="lookup_unique_idx", columns={
* "locale", "object_id", "field"
* })}
* )
*/
class BookTranslation extends AbstractPersonalTranslation
{
/**
* Convinient constructor
*
* @param string $locale
* @param string $field
* @param string $value
*/
public function __construct($locale = null, $field = null, $value = null)
{
$this->setLocale($locale);
$this->setField($field);
$this->setContent($value);
}
/**
* @ORM\ManyToOne(targetEntity="Book", inversedBy="translations")
* @ORM\JoinColumn(name="object_id", referencedColumnName="id", onDelete="CASCADE")
*/
protected $object;
}
<?php
namespace Demo\BookBundle\Admin;
use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
/**
* BookTranslation Admin
*/
class BookTranslationAdmin extends Admin
{
/**
* Configure the list
*
* @param \Sonata\AdminBundle\Datagrid\ListMapper $list list
*/
protected function configureListFields(ListMapper $list)
{
$list
->addIdentifier('field', null, array('label' => 'Champ'))
->add('content', null, array('label' => 'Traduction'))
->add('locale', null, array('label' => 'Actif'))
// add custom action links
->add('_action', 'actions', array(
'actions' => array(
'edit' => array()
)
));
}
/**
* Configure the form
*
* @param FormMapper $formMapper formMapper
*/
public function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('field', null, array('label' => 'Field'))
->add('content', null, array('label' => 'Translation'))
->add('locale', null, array('label' => 'locale'));
}
}
@luismiguens
Copy link

Hello Tristan,

I'm trying to implement this translation example with SonataAdminBundle & DoctrineExtensions, but when I run the create action form, the database translation field object_id is empty. All other fields are filled correctly on database!

My database schema:

CREATE TABLE Book (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL,
description longtext,
PRIMARY KEY (id),
UNIQUE KEY UNIQ_6BD70C0F5E237E06 (name)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1

CREATE TABLE book_translations (
id int(11) NOT NULL AUTO_INCREMENT,
object_id int(11) DEFAULT NULL,
locale varchar(8) COLLATE utf8_unicode_ci NOT NULL,
field varchar(32) COLLATE utf8_unicode_ci NOT NULL,
content longtext COLLATE utf8_unicode_ci,
PRIMARY KEY (id),
UNIQUE KEY lookup_unique_idx (locale,object_id,field),
KEY IDX_9F5610DF232D562B (object_id),
CONSTRAINT FK_9F5610DF232D562B FOREIGN KEY (object_id) REFERENCES Book (id) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

Can you give me a help? I have the same source!!!

Thanks in advance, Luis Miguens

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment