I would like to change entity value in case if it's for example NULL. Unfortunately my code causes exception - it seems that id isn't set although 'translation' record is created in database and id is properly returned by getId method.
It's quite simple code, why it doesn't work?
public function createAction(Request $request)
{
$entity = new Word();
$form = $this->createForm(new WordType(), $entity);
$form->bind($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
//works fine - database record is created
if($entity->getIdTranslation() == NULL){
$translation = new Translation();
$em->persist($translation);
$em->flush();
$entity->setIdTranslation($translation->getId());
}
$em->persist($entity);
//throws exception - Integrity constraint violation: 1048 Column 'id_translation' cannot be null
$em->flush();
return $this->redirect($this->generateUrl('admin_word_show', array('id' => $entity->getId())));
}
return array(
'entity' => $entity,
'form' => $form->createView(),
);
}
Edit: added part of my model and mappings info:

Revelant Words mappings:
/**
* @var integer $id_language
*
* @ORM\Column(name="id_language", type="integer")
*/
private $id_language;
/**
*@ORM\ManyToOne(targetEntity="Language", inversedBy="words")
*@ORM\JoinColumn (name="id_language", referencedColumnName="id")
*/
protected $language;
/**
*
* @ORM\ManyToOne(targetEntity="Translation", inversedBy="words")
* @ORM\JoinColumn(name="id_translation", referencedColumnName="id")
*/
protected $translation;
and Translations:
class Translation
{
public function __construct() {
$this->words = new ArrayCollection();
}
/**
* @ORM\PrePersist()
*/
public function prePersist() {
$this->created_date = new \DateTime();
}
/**
*
* @ORM\OneToMany(targetEntity="Word" ,mappedBy="translation")
*/
protected $words;
Also on side note: I'v created my db model using database modeling software (workbench) not by Symfony console tools and now I'm trying to setup Entities to reflect db model. I'm not sure if its right approach - maybe my model is too complicated to work properly with Doctrine?