简体   繁体   English

类表继承的实现

[英]Class table inheritance implementation

I'm working on a project and I've come out with this doubt. 我正在做一个项目,但我对此提出了疑问。 I've got an IS-A relationship which looks like this: 我有一个IS-A关系,看起来像这样:

Users as a parent table, then Companies, Admins and Customers as child tables. Users作为父表,然后Companies, Admins and Customers作为子表。 Since all of them have different attributes I've decided to create 4 tables and link them to the Users table which will hold the login information, which is the common information between the childs. 由于它们都有不同的属性,因此我决定创建4个表并将它们链接到Users表,该表将保存登录信息,这是子级之间的常用信息。

My struggle is that I can't create a child if the parent doesn't exist obviusly, but I'd like to let the customers and companies register themselves, so the solution I've come out with is inserting a new user , then get the max id from users and insert a customer . 我的挣扎是,如果父母不存在,我就无法创建孩子,但我想让客户和公司进行注册,因此我想出的解决方案是插入新user ,然后从users那里获得最大ID并插入一个customer But I'm not sure if is a good approach, what happens if multiple people try to register at once? 但是我不确定这是否是一种好方法,如果多个人同时尝试注册会怎样?

Could I control the parent insertion from the database instead of my code? 我可以控制数据库而不是代码中的父项插入吗? Maybe with a trigger? 也许有触发条件?

That would make more sense to me, mainly because an user itself doesn't make any sense in my context, it's either a customer, an admin or a company. 这对我来说更有意义,主要是因为用户本身对我而言没有任何意义,它可以是客户,管理员或公司。

Given that, like you say, a user itself does not make any sense, I think your user class should be made abstract. 鉴于您所说的那样,用户本身没有任何意义,我认为您的用户类应该抽象化。 This way you don't have to create any User, the information is directlu inherited from the User class in your children entities. 这样,您无需创建任何用户,信息就直接继承自子实体中的User类。

Detailed edit : From a doctrine point of view, simply use the @ORM\\MappedSuperclass annotation on you parent entity (User). 详细编辑:从学说的角度来看,只需在父实体(用户)上使用@ORM\\MappedSuperclass批注。 There is nothing to do in your children entities. 您的子实体中无事可做。 This way, every child entity will inherit all the properties and methods of the parent entity, which in your case is what I understood you wanted to do. 这样,每个子实体都将继承父实体的所有属性和方法,在您的情况下,这就是我理解的要执行的操作。 I let you read the specific part of the doctrine documentation about this point : 我让您阅读有关这一点的文档文档的特定部分:

Doctrine inheritance mapped superclass 主义继承映射超类

Hope this helps. 希望这可以帮助。

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

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