如何修复有关系的我的实体的删除?

时间:2019-07-02 09:55:11

标签: doctrine symfony4

我要删除带有外键的实体

属性onDelete = cascade不起作用

Client.php


    /**
     * @var User
     *
     * @ORM\OneToOne(targetEntity="User", inversedBy="client")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id",  onDelete="cascade")
     */
    private $user;

User.php


    /**
     * @var Client
     *
     * @ORM\OneToOne(targetEntity="Client", mappedBy="user")
     * @ORM\JoinColumn(name="client_id", referencedColumnName="id", onDelete="CASCADE")
     */
    private $client;

ClientController.php

/**
     * @Route("/clients/{id}/delete", name="client_delete")
     * @Security("has_role('ROLE_ADMIN')")
     * @Method("POST")
     */
    public function clientDeleteAction(Client $client)

        {
            $em = $this->get('doctrine')->getManager();

            $em->remove($client);

            $em->flush();

            return ($this->redirectToRoute("client_list"));
        }

错误是:

SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`aliance_auto`.`user`, CONSTRAINT `FK_8D93D64919EB6921` FOREIGN KEY (`client_id`) REFERENCES `client` (`id`))

0 个答案:

没有答案