I have 2 entities that are related with a OneToOne relationship. I embeded the inverse side into the form, and everything works fine, except that the link between the relationship is not stored. So: The "Begeleider" is saved and the "CompetentieProfiel" is saved, but the column that references the "Begeleider" inside "CompetentieProfiel" table is null.
Ath the moment flush() is called, the "Begeleider" object has the "CompetentieProfiel" object as variable.
Contact:
class Contact { /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ private $id; } Begeleider:
class Begeleider extends Contact { /** * @ORM\OneToOne(targetEntity="CompetentieProfiel", mappedBy="begeleider" ,cascade={"persist"}) */ private $competentieProfiel; } CompetentieProfiel:
class CompetentieProfiel { /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @ORM\OneToOne(targetEntity="Begeleider", inversedBy="competentieProfiel",cascade={"persist"}) */ protected $begeleider; } Form:
class BegeleiderType extends AbstractType { /** * @param FormBuilderInterface $builder * @param array $options */ public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('competentieProfiel', new CompetentieProfielType()); } Controller:
public function createAction(Request $request) { $entity = new Begeleider(); $form = $this->createCreateForm($entity); $form->handleRequest($request); if ($form->isValid()) { $em = $this->getDoctrine()->getManager(); $em->persist($entity); $em->flush(); // Return the ok status and the begeleider html. $response = new Response( json_encode( array( 'status' => 'ok', ) ) ); $response->headers->set('Content-Type', 'application/json'); return $response; }