我有两个名为Doctor和Users的表。用户表包含有关医生,患者和管理员的详细信息。医生表包含有关医生的详细信息这些表彼此链接。我将从twig文件中删除医生的ID。有了这个id,我可以删除Doctor表中的数据,但是如何删除Users表中同一个医生的数据呢?有人可以告诉我代码。
控制器
public function doctorsAction(Request $request,$id)
{
$user = $this->get('security.context')->getToken()->getUser();
if($id){
$doctors = $this->getDoctrine()->getRepository('ProjectBundle:Doctors')->findOneBy(array('id' => $id));
$em = $this->getDoctrine()->getEntityManager();
$em->remove($doctors);
$em->flush();
return $this->redirect($this->generateUrl('admin_doctors'));
}
$searchDate = $request->query->get('date');
$results = $this->getDoctrine()->getManager()->getRepository('ProjectBundleBundle:Users')->findUserDoctor($request->query->get('search'), $searchDate);
$form = $this->createForm(new UsersType(), new Users());
return array('doctors' => $results, 'form' => $form->createView(), 'searchDate' => ($searchDate ? $searchDate : date('Y-m-d')));
}
答案 0 :(得分:0)
您应该通过级联删除在Doctor和User对象之间建立一对一的ORM关系。
YouBundle \实体\ Doctor.php
/**
* @var User
*
* @ORM\OneToOne(targetEntity="User")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id", onDelete="cascade")
*/
protected $user.