Doctrine类表继承不能删除()实体

时间:2018-01-12 14:33:57

标签: php symfony doctrine-orm doctrine class-table-inheritance

我尝试实现Doctrine的类表继承。我的应用程序需要一个用户实体,用于通过Symfony的安全系统对用户进行身份验证。最重要的是,我的应用程序需要一个特殊的用户,一个医生。 您可以在下面找到我的实体类的摘录。

用户类(基础):

/**
 * @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扩展用户:

/**
 * @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

}

奇怪的是,我既不能使用EntityManager的Users方法删除Doctors也不能删除remove(),我从来没有遇到任何问题。当我使用该方法时,不会抛出或记录错误,但实体实例仍保留在数据库中。

// 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);
}

我找到了一种解决方法,使用手动查询从数据库中删除对象。

// 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;

所以我可以使用上面的代码,但我仍然想知道导致此问题的原因是什么? 提前谢谢!

2 个答案:

答案 0 :(得分:0)

  

如果不使用SchemaTool生成所需的SQL,则应该知道删除类表继承会在所有数据库实现中使用外键属性import tkinter as tk from PIL import Image, ImageDraw, ImageTk class App(tk.Tk): def __init__(self): tk.Tk.__init__(self) self.title("Dynamic Image Test") tk.Label(self, text="Overlay").grid(column=1, row=1, sticky="nesw") tk.Label(self, text="Token").grid(column=2, row=1, sticky="nesw") tk.Label(self, text="Combined").grid(column=3, row=1, sticky="nesw") self.label_1 = tk.Label(self) self.label_1.grid(column=1, row=2, sticky="nesw") self.label_2 = tk.Label(self) self.label_2.grid(column=2, row=2, sticky="nesw") self.label_3 = tk.Label(self) self.label_3.grid(column=3, row=2, sticky="nesw") self.images = [None, None, None] self.photos = [None, None, None] self.show_changes() def overlay(self): im = Image.new("RGBA", (100,100), (255,255,0,255)) draw = ImageDraw.Draw(im) draw.ellipse((10,10,90,90),fill=(0,0,0,0)) return im def token(self): im = Image.new("RGBA", (100,100), (0,0,0,0)) draw = ImageDraw.Draw(im) draw.ellipse((0,0,100,100),fill=(0,0,255,255)) draw.line((15,15,85,85),fill=(255,0,0,255), width=5) draw.line((15,85,85,15),fill=(255,0,0,255), width=5) return im def combine(self, overlay, token): return Image.alpha_composite(token, overlay) def show_changes(self): self.images[0] = self.overlay() self.photos[0] = ImageTk.PhotoImage(self.images[0]) self.label_1.configure(image=self.photos[0]) self.images[1] = self.token() self.photos[1] = ImageTk.PhotoImage(self.images[1]) self.label_2.configure(image=self.photos[1]) self.images[2] = self.combine(self.images[0], self.images[1]) self.photos[2] = ImageTk.PhotoImage(self.images[2]) self.label_3.configure(image=self.photos[2]) if __name__ == "__main__": app = App() app.mainloop() 。如果未能自行实现,将导致数据库中出现死行。

参考: Doctrine 2 documentation

如下所示运行此SQL查询可能会解决您的问题

ON DELETE CASCADE

答案 1 :(得分:0)

相关问题
最新问题