我需要将许多实体映射为一对多到同一个实体主键。symfony2 + doctrine2:映射许多实体加入同一个实体复合主键
为了简便起见,我将尝试semplify一点点:
假设我有一个新闻和产品表需要进行不同的排序,如果显示在网站或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非常相似的情况,但我无法将其应用于我的情况。
感谢mahok,很好的建议! – 2014-03-04 18:13:32