Reputation: 295
I have a OneToOne relation between 2 entities. One Molecule and one ConcGenotox (conclusion about genotoxicity).
I come from a page where all imformations about one Molecule are displayed and the id of the Molecule is in the address bar.
There is a button to edit a new ConcGenotox about this molecule witch id is always in the address bar.
My question is : How to add automatically the id from the molecule in the id_molecule relation of ConcGenotox when adding a new ConcGenotox ?
ConcGenotox Entity:
class ConcGenotox
{
/**
* @var \Molecule
*
* @ORM\OneToOne(targetEntity="Molecule")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="id_molecule", referencedColumnName="id")
* })
*
*/
private $idMolecule;
Molecule Entity :
class Molecule
{
/**
* @ORM\OneToOne(targetEntity="NcstoxBundle\Entity\ConcGenotox", mappedBy="idMolecule")
*/
private $concGenotox;
Contrôler:
/**
* Creates a new concGenotox entity.
*
* @Route("/new/{id}", name="concgenotox_new")
* @Method({"GET", "POST"})
*/
public function newAction(Request $request, $id)
{
$concGenotox = new Concgenotox();
$form = $this->createForm('NcstoxBundle\Form\ConcGenotoxType', $concGenotox);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
var_dump($id);
$concGenotox->setIdMolecule($id);
$em->persist($concGenotox);
$em->flush();
return $this->redirectToRoute('concgenotox_show', array('id' => $concGenotox->getId()));
}
return $this->render('concgenotox/new.html.twig', array(
'concGenotox' => $concGenotox,
'form' => $form->createView(),
'id' => $id,
));
}
I tried to ->setIdMolecule($id)
but it give me an error :
Expected value of type "NcstoxBundle\Entity\Molecule" for association field "NcstoxBundle\Entity\ConcGenotox#$idMolecule", got "string" instead.
Upvotes: 1
Views: 74
Reputation: 974
Relationship property should be entity, not id.
Change:
class ConcGenotox
{
/**
* @var \Molecule
*
* @ORM\OneToOne(targetEntity="Molecule")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="id_molecule", referencedColumnName="id")
* })
*
*/
private $idMolecule;
to:
class ConcGenotox
{
/**
* @var \Molecule
*
* @ORM\OneToOne(targetEntity="Molecule", cascade={"persist"})
*/
private $molecule;
and change your action to:
/**
* Creates a new concGenotox entity.
*
* @Route("/new/{molecule}", name="concgenotox_new")
* @Method({"GET", "POST"})
*/
public function newAction(Request $request, Molecule $molecule)
{
$concGenotox = new Concgenotox();
$form = $this->createForm(ConcGenotoxType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$concGenotox = $form->getData();
$molecule->setConcGenotox($concGenotox);
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('concgenotox_show', array('id' => $concGenotox->getId()));
}
return $this->render('concgenotox/new.html.twig', array(
'concGenotox' => $concGenotox,
'form' => $form->createView(),
'id' => $id,
));
}
Upvotes: 1
Reputation: 35973
try this:
$em = $this->getDoctrine()->getManager();
$molecule = $em->getRepository('YourBundle:Molecule')
->find($id);
$concGenotox->setIdMolecule($molecule);
You need to set the entity not the Id
Upvotes: 2