简体   繁体   English

教义类表继承不能为remove()实体

[英]Doctrine Class Table Inheritance can not remove() entity

I try to implement Doctrine's Class Table Inheritance. 我尝试实现Doctrine的类表继承。 My Application needs a User Entity that is used to authenticate the user through Symfony 's security system. 我的应用程序需要一个用户实体,该实体用于通过Symfony的安全系统对用户进行身份验证。 On top of that, my application needs a special kind of User, a Doctor. 最重要的是,我的应用程序需要一种特殊的用户,即Doctor。 Below you can find excerpts of my Entity classes. 您可以在下面找到我的Entity类的节选。

User class (Base): 用户类别(基本):

/**
 * @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
 * @ORM\Table(name="user")
 * @UniqueEntity("email")
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="discr", type="string")
 * @ORM\DiscriminatorMap({"user" = "User", "doctor" = "Doctor"})
 */
class User implements UserInterface, \Serializable, EquatableInterface {

/**
 * @ORM\Id()
 * @ORM\Column(name="id", type="uuid_binary")
 * @ORM\GeneratedValue(strategy="CUSTOM")
 * @ORM\CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidGenerator")
 * @JMS\Type("uuid")
 */
private $id;

// More fields that are used for authenticating a user (password, email, etc.)

}

Doctor Entity extends User: Doctor Entity扩展了用户:

/**
 * @ORM\Entity(repositoryClass="AppBundle\Repository\DoctorRepository")
 * @ORM\Table(name="doctor")
 * @ORM\HasLifecycleCallbacks()
 */
class Doctor extends User {

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

// More fields that extend the User Entity

}

Weirdly I can neither delete Users nor the Doctors using the EntityManager's remove() method, which I have never had problems with. 奇怪的是,我无法使用EntityManager的remove()方法删除“ Users或“ Doctors ”,而我从来没有遇到过问题。 When I use the method no errors are thrown or logged, but the Entity instance remains in the database. 当我使用该方法时,不会引发或记录任何错误,但是Entity实例仍保留在数据库中。

// This does not work. The user stays persisted in the database.
public function deleteUserAction($id) {
    $em = $this->getDoctrine()->getManager();
    $user = $em->getRepository('AppBundle:User')->find($id);
    if(empty($user)) {
        return new View('User not found', Response::HTTP_NOT_FOUND);
    }
    $em->remove($user);
    $em->flush();
    return new View('Deleted user', Response::HTTP_OK);
}

I found a workaround using a manual query to delete the object from the database. 我找到了一种解决方法,使用手动查询从数据库中删除对象。

// This works. The User is deleted from the database.
// If the user is a doctor the doctor referencing the user id is also
// deleted.
$qb = $em->createQueryBuilder()
        ->delete('AppBundle:User', 'u')
        ->where('u.id = :id')
        ->setParameter('id', $id, 'uuid_binary');
$qb->getQuery()->execute();
return $user;

So I could just use the code above, but I still would like to know what causes this problem to occur? 所以我可以只使用上面的代码,但是我仍然想知道是什么原因导致此问题发生? Thanks in advance! 提前致谢!

When you do not use the SchemaTool to generate the required SQL you should know that deleting a class table inheritance makes use of the foreign key property ON DELETE CASCADE in all database implementations. 当您不使用SchemaTool生成所需的SQL时,您应该知道删除类表继承会在所有数据库实现中使用外键属性ON DELETE CASCADE A failure to implement this yourself will lead to dead rows in the database. 自己无法实现此操作将导致数据库中的行失效。

Reference: Doctrine 2 documentation : 参考: Doctrine 2文档

Running this SQL query like following may solve your problem 像下面那样运行此SQL查询可以解决您的问题

ALTER TABLE user 
ADD CONSTRAINT fk_user_1
FOREIGN KEY (id_user)
REFERENCES doctor (id_user)
ON DELETE CASCADE
ON UPDATE NO ACTION;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM