我有 2 个实体患者
class Patients
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\OneToOne(targetEntity="ContactAddress", mappedBy="patient" ,cascade={"persist"})
*/
protected $contactaddress;
和联系地址
class ContactAddress
{
/**
* @ORM\OneToOne(targetEntity="Patients", inversedBy="contactaddress")
* @ORM\JoinColumn(name="patient_id", referencedColumnName="id")
*/
protected $patient;
}
我确实嵌入了表单它几乎可以工作,但只有字段 patient_id 为 NULL。我希望患者的 id 将是 ContactAddress 中的外键我的控制器的一部分
$entity = new Patients();
$request = $this->getRequest();
$form = $this->createForm(new \Surgery\PatientBundle\Form\PatientsType(), $entity);
$form->bindRequest($request);
if($request->getMethod()=='POST') //sending form
{
if ($form->isValid()) //valid form
{
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('kategoria_show', array('id' => $entity->getId())));
}
编辑:我改变了关联
/**
* @ORM\OneToOne(targetEntity="Patients", inversedBy="patients")
* @ORM\JoinColumn(name="patient_id", referencedColumnName="id")
*/
protected $patient;
/**
* @ORM\OneToOne(targetEntity="ContactAddress", mappedBy="contactaddress" ,cascade={"persist"})
*/
protected $contactaddress;
但它仍然不起作用,我在 ContactAddress 中的字段 patient_id 为 NULL