Hello,
I'm facing a very strange behavior of translatable.
I followed the informations i found to add the traits to my entity, then i created a translation 'entity'.
I then used the generate:crud function from symfony console.
But when i try to create a new record, the translation is not put in the database, only the CsVictims record is created.
I modified my crud code to have access to the debugger after the creation and i get this as doctrine calls :
"START TRANSACTION"
INSERT INTO CsVictims (id)
VALUES
(null)
"COMMIT"
The mapping display no mapping errors.
i have added this line in my config.yml :
imports:
- { resource: ../../vendor/knplabs/doctrine-behaviors/config/orm-services.yml }
My entity is the following :
<?php
namespace myproject\WebsiteBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Knp\DoctrineBehaviors\Model as ORMBehaviors;
/**
* CsVictims
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="myproject\WebsiteBundle\Entity\CsVictimsRepository")
*/
class CsVictims
{
use ORMBehaviors\Translatable\Translatable;
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToMany(targetEntity="CsDisease", mappedBy="victims")
*/
protected $diseases;
protected $description;
protected $name;
public function __construct()
{
$this->diseases = new ArrayCollection();
}
public function __toString() {
if ($this->getName() == null) {
return "";
}
return $this->getName();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
public function setDiseases($diseases)
{
$this->diseases = $diseases;
}
public function getDiseases()
{
return $this->diseases;
}
public function getName() {
return $this->translate($this->getCurrentLocale())->getName();
}
public function setName($name) {
$this->translate($this->getCurrentLocale())->setName($name);
return $this;
}
public function getDescription() {
return $this->translate($this->getCurrentLocale())->getDescription();
}
public function setDescription($desc) {
$this->translate($this->getCurrentLocale())->setDescription($desc);
return $this;
}
}
The translation 'entity' is the following :
<?php
namespace myproject\WebsiteBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Model as ORMBehaviors;
/**
* CsVictimsTranslation
*
* @ORM\Entity
*/
class CsVictimsTranslation
{
use ORMBehaviors\Translatable\Translation;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255, nullable=true)
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="description", type="string", length=255, nullable=true)
*/
private $description;
public function __toString() {
return $this->name;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return CsVictims
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set description
*
* @param string $description
* @return CsVictims
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
}
The form type that was generated by the crud is the following :
<?php
namespace myproject\WebsiteBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class CsVictimsType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('description')
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'myproject\WebsiteBundle\Entity\CsVictims'
));
}
public function getName()
{
return 'myproject_websitebundle_csvictimstype';
}
}
and finally the controller code is the following (i modified the redirection to see what is happening with the debugguer tool) :
<?php
namespace myproject\WebsiteBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use myproject\WebsiteBundle\Entity\CsVictims;
use myproject\WebsiteBundle\Form\CsVictimsType;
/**
* CsVictims controller.
*
*/
class CsVictimsController extends Controller
{
...
/**
* Creates a new CsVictims entity.
*
*/
public function createAction(Request $request)
{
$entity = new CsVictims();
$form = $this->createForm(new CsVictimsType(), $entity);
$form->bind($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
//return $this->redirect($this->generateUrl('crud_csvictims_show', array('id' => $entity->getId())));
}
return $this->render(myprojectWebsiteBundle:CsVictims:new.html.twig', array(
'entity' => $entity,
'form' => $form->createView(),
));
}
...
Somme additionnal informations from the debugger :
Request POST Parameters
Key
myproject_websitebundle_csvictimstype
Value
{"name":"testEnglish1","description":"testEnglish2","_token":"f42fb730b0f96a71d140d7dd396ee28b46b192ad"}
_locale "en"
I'm fighting with this code and i'm trying to understand what can be wrong out there.
Did i miss something about the listener ? Should i register it manually in any other way than importing the yml file in config.yml ?
I may be doing a big mistake somewhere but i can't find where, so any help would be really appreciated.