我需要将许多实体作为一对多映射到同一个实体主键。
为了简洁起见,我将尝试简化一点:
假设我有一个新闻和一个产品表,如果显示在网站或 ios 应用程序中,则需要以不同的方式订购。我希望只有一个表来存储所有表的订单信息(称为“位置”),因为将来我需要管理其他 20 个或更多表。
以下是简化表格:
_________
|news |
---------
|id (pk) |
|title |
---------
_________
|product |
---------
|id (pk) |
|name |
---------
__________
|position | id + table + target are the composite primary key
----------
|id (pk) | can refer to a news.id or to a product.id depending on table field value
|table(pk) | define if the id refer to a news or to a product
|target(pk)| can be "web" or "ios"
|position | an integer
----------
3 possible position records could be:
id - table - position - target
1 news 1 web
1 news 1 ios
1 product 1 web
如您所见,我可以将相同的 id 重复 3 次。我知道这有点脏,也许不是最佳实践,但每次我需要添加表格(新闻、艺术家、活动等)时,它都会为我节省大量的表格和实体创建。我知道使用 Doctrine2 来满足我的需求的唯一方法是创建一个 xxx_position_web 和一个 xxx_position_ios 表以及我将来必须管理的每个新表的相关实体。
是否可以在 Symfony2 中使用 Doctrine2 实体来管理这样的事情。我找到了一个链接来解释与我的情况非常相似的情况https://doctrine-orm.readthedocs.org/en/latest/tutorials/composite-primary-keys.html?highlight=composite#identity-through-foreign-entities但是我无法将其应用于我的情况。