Symfony2级联更新

时间:2015-08-27 15:35:51

标签: entity-framework symfony doctrine-orm

我正在使用带有Doctrine的Symfony2.7,并且在更新实体的id后出现问题,这就是问题所在:

An exception occurred while executing 'UPDATE Vmrs SET id = ? WHERE id = ?' with params [14, 8]:
SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`stageocp`.`vmr_reservation`, CONSTRAINT `FK_755A06C0552D88B6` FOREIGN KEY (`vmr_id`) REFERENCES `vmrs` (`id`))

我对删除操作没有问题,它删除了实体中的字段以及带有外键的实体中的所有字段,我想对更新进行同样的操作。

当具有外键的实体没有共享我更新的同一实体的字段时,它可以正常工作。

这是我的代码:

包含外键的实体:

/**
* VmrReservation
*
* @ORM\Table()
*@ORM\Entity(repositoryClass="StageOCP\ProjectBundle\Repository\VmrReservationRepository")
*/
class VmrReservation {
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @ORM\OneToMany(targetEntity="StageOCP\ProjectBundle\Entity\Guest", mappedBy="vmrreservation", cascade={"persist","remove"})
 */
private $guests;

/**
 * @ORM\ManyToOne(targetEntity="StageOCP\UserBundle\Entity\User", inversedBy="vmrreservations")
 * @ORM\JoinColumn(nullable=true)
 */
private $user;

/**
 * @ORM\ManyToOne(targetEntity="StageOCP\ProjectBundle\Entity\Reservation", inversedBy="vmrreservations")
 * @ORM\JoinColumn(nullable=false)
 */
private $reservation;

/**
 * @ORM\ManyToOne(targetEntity="StageOCP\ProjectBundle\Entity\Vmr", inversedBy="vmrreservations", cascade={"persist","remove"})
 * @ORM\JoinColumn(nullable=false)
 */
private $vmr;

/**
 * @var boolean
 *
 * @ORM\Column(name="isReserved", type="boolean")
 */
private $isReserved;


/**
 * Get id
 *
 * @return integer
 */
public function getId() {
    return $this->id;
}

包含主键(我更新的主键)的实体:

/**
* Vmrs
*
* @ORM\Table(name="Vmrs")
* @ORM\Entity(repositoryClass="StageOCP\ProjectBundle\Repository\VmrsRepository")
*/
class Vmr
{
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @ORM\OneToMany(targetEntity="StageOCP\ProjectBundle\Entity\VmrReservation", mappedBy="vmr", cascade={"persist","remove"})
 */
private $vmrreservations;

/**
 * @var string
 *
 * @ORM\Column(name="name", type="string", length=255)
 */
private $name;


/**
 * Get id
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
}

/**
 * Set id
 *
 * @param int $id
 * @return Vmrs
 */
public function setId($id)
{
    $this->id = $id;

    return $this;
}

这是我的控制器:

/**
 * Displays a form to edit an existing Vmr entity.
 *
 */
public function editAction($id) {
    $em = $this->getDoctrine()->getManager();

    $entity = $em->getRepository('StageOCPProjectBundle:Vmr')->find($id);

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

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

    return $this->render('StageOCPProjectBundle:Vmr:edit.html.twig', array(
        'entity' => $entity,
        'edit_form' => $editForm->createView(),
        'delete_form' => $deleteForm->createView(),
    ));
}
public function updateAction(Request $request, $id) {
    $em = $this->getDoctrine()->getManager();

    $entity = $em->getRepository('StageOCPProjectBundle:Vmr')->find($id);

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

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

    if ($editForm->isValid()) {
        $metadata = $em->getClassMetaData(get_class($entity));
        $metadata->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_NONE);
        $em->flush();

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

    return $this->render('StageOCPProjectBundle:Vmr:edit.html.twig', array(
        'entity' => $entity,
        'edit_form' => $editForm->createView(),
        'delete_form' => $deleteForm->createView(),
    ));
}

当我更新实体vmr中的主键(id)时,我想更新实体vmrresevation中的外键。

我希望我能找到解决方案,谢谢

0 个答案:

没有答案