简体   繁体   English

更多关于symfony2的理论和关系

[英]more on symfony2 doctrine and relations

So now I have set up my slideshow containers like: Geoarea, Building, Server 所以现在我已经设置了幻灯片容器,例如:Geoarea,Building,Server

in different entities, mapped from one superclass. 从一个超类映射的不同实体中。

Now the next problem I face is: I want to be able to relate these containers' contents to either one of the containers... 现在我面临的下一个问题是:我希望能够将这些容器的内容与其中一个容器相关联...

That was the reason I was initially just contemplating having one container entity and setting containertype with an INT flag, but OO-wise that isn't smooth or correct I think. 这就是为什么我最初只是考虑拥有一个容器实体并使用INT标志设置containertype的原因,但是从OO角度来看,这并不顺利或不正确。

Now, I could of course have FK fields in my entity 'content' for each of the possible containers, and set them to nullable and only use one of them, but I feel like I am stuck in a rut here, think outside the box... I want to make fetching a container and it's related contents as simple as possible... 现在,我当然可以在实体的“内容”中为每个可能的容器添加FK字段,并将它们设置为可为空,并且仅使用其中一个,但是我觉得我陷入了困境,请在框外思考...我想使获取容器及其相关内容尽可能简单...

Each content is only related to ONE container, so having 3 FK-fields when never using more than one feels dumb... 每个内容仅与一个容器相关,因此拥有3个FK字段(从不使用多个)会让人感到愚蠢……

You have two solutions. 您有两种解决方案。 The first is to use Inheritance : Inheritance Mapping , but you will have some redundance in your database. 第一种是使用Inheritance: Inheritance Mapping ,但是您的数据库中会有一些冗余。

The second solution is to add a FK in your Child entity and not three in your Mother entity. 第二种解决方案是在您的子实体中添加FK,而不在您的母实体中添加FK。 Thereby, you will store common data in your Mother superclass. 因此,您将把公共数据存储在您的母亲超类中。

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

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