gpt4 book ai didi

php - 使用 OneToMany 删除实体

转载 作者:行者123 更新时间:2023-11-29 12:06:15 25 4
gpt4 key购买 nike

我有实体开发人员

/**
* Developers
*
* @ORM\Table(name="developers")
* @ORM\Entity(repositoryClass="Artel\CustomerBundle\Entity\Repository\DevelopersRepository")
*/
class Developers extends SUser
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;

/**
* @ORM\OneToMany(targetEntity="CodeUserReference", mappedBy="developer", cascade={"persist"})
*/
protected $reference_developer;

和实体代码UserReference

 /**
* Coders 24 users
*
* @ORM\Table(name="code_user_reference")
* @ORM\Entity(repositoryClass="Artel\CustomerBundle\Entity\CodeUserReferenceRepository")
*/
class CodeUserReference
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;

/**
* @ORM\ManyToOne(targetEntity="Developers", inversedBy="reference_developer")
* @ORM\JoinColumn(name="user_talnet_file", nullable = true, referencedColumnName="id")
* */
protected $developer;

我的问题如何删除开发者我有错误

An exception occurred while executing 'DELETE FROM developers WHERE id = ?' with params [559]:

SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`artel`.`code_user_reference`, CONSTRAINT `FK_E57A030D93D16BFC` FOREIGN KEY (`user_talnet_file`) REFERENCES `developers` (`id`))

这是我的行动

   /**
* Displays a form to edit an existing Freelancers entity.
*
* @Route("/{id}/edit", name="admin_freelancers_edit")
* @Method("GET")
* @Template()
*/
public function editAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('ArtelCustomerBundle:Developers')->find($id);

if (!$entity) {
throw $this->createNotFoundException('Unable to find Freelancers entity.');
}

$editForm = $this->createEditForm($entity);
$deleteForm = $this->createDeleteForm($id);

return array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
);
}
/**
* Creates a form to delete a Freelancers entity by id.
*
* @param mixed $id The entity id
*
* @return \Symfony\Component\Form\Form The form
*/
private function createDeleteForm($id)
{
return $this->createFormBuilder()
->setAction($this->generateUrl('admin_freelancers_delete', array('id' => $id)))
->setMethod('DELETE')
->add('submit', 'submit', array('label' => 'Delete'))
->getForm()
;
}
/**
* Deletes a Freelancers entity.
*
* @Route("/{id}", name="admin_freelancers_delete")
* @Method("DELETE")
*/
public function deleteAction(Request $request, $id)
{
$form = $this->createDeleteForm($id);
$form->handleRequest($request);

if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('ArtelCustomerBundle:Developers')->find($id);


if (!$entity) {
throw $this->createNotFoundException('Unable to find Freelancers entity.');
}

$em->remove($entity);
$em->flush();
}

return $this->redirect($this->generateUrl('admin_freelancers'));
}

在开发人员没有字段reference_developer和actionwork之前,我添加了这个字段和表,现在我有开发人员和开发人员有更多的一两个或五个引用,我不知道如何用他的引用来纠正开发人员?请帮忙

最佳答案

您的问题是开发人员已有现有的recorCodeUserReference。您需要更新您的开发人员 OneToMany 以包含级联“删除”。

/**
* @ORM\OneToMany(targetEntity="CodeUserReference", mappedBy="developer", cascade={"persist", "remove"})
*/
protected $reference_developer;

这里有很好的解释:On delete cascade with doctrine2

关于php - 使用 OneToMany 删除实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31429540/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com