多对一与多个目标实体 [英] Many-To-One with multiple target entities

查看:84
本文介绍了多对一与多个目标实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎很简单,但我不能正确:



有三个实体: Fruit Vegetable Snack 。 Snack有字段 id 时间 food 。食物是指 一个水果或一种蔬菜。所以它基本上是一个多对一/一对多的关系,因为一个小吃总是只有一个食物。但是有多个目标实体。



如何在Doctrine2中映射?



我会在使用之前知道Doctrine2将使用两个字段: food_type food_id 。但如何才能从食物类型到正确的实体连接?我想到一个JoinColumns数组,但是找不到一种方法来连接正确的实体。我还看看映射的超类,因为有一个DiscriminatorColumn,但它也似乎是错误的方法。如果我得到它正确的超类不能是一个实体本身 - 所以我不能创建一个食物实体。



任何帮助是赞赏。

解决方案

您可以创建一个(抽象)映射的超类名为 Food ,它可以保存水果蔬菜的一些基本信息。 p>

您的问题的关键字是继承映射,这是它的文档: http://doctrine-orm.readthedocs.org/en/latest/reference/inheritance-mapping.html



然后,您可以在实体关系中引用此映射的超类。


This seems simple but I can't get it right:

There are three entities: Fruit, Vegetable and Snack. Snack has the fields id, time and food. Food is a reference to either one fruit or one vegetable. So it is basically a many-to-one/one-to-many relationship as one snack will always only hold one food. But there is more than one target entity.

How would I map this in Doctrine2?

A simple solution I would have used before knowing Doctrine2 would be to use two fields: food_type and food_id. But how can I make a connection from food type to the correct entity? I thought about an array of JoinColumns but can't find a way to connect the correct entity. I also had a look at mapped superclasses because there is a DiscriminatorColumn, but it also seems to be the wrong approach. If I get it right the superclass can't be an entity itself - so I cannot create a food entity.

Any help is appreciated. I'm sure I am missing something simple here.

解决方案

You can create a (abstract) mapped superclass called Food, which can hold some basic information for Fruit and Vegetable.

The keyword for your question is inheritance mapping, this is the documentation for it: http://doctrine-orm.readthedocs.org/en/latest/reference/inheritance-mapping.html

Then you could reference this mapped superclass in your entity relationship.

这篇关于多对一与多个目标实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆