[英]How do I model my ER diagram when Doctrine won't let me associate composite primary key with another entity's foreign key?
I am trying to model the ManyToOne relations between 我正在尝试建模之间的ManyToOne关系
product_pricename
and category_pricename
product_pricename
和category_pricename
price_history
and product_pricename
, as in the diagram below: price_history
和product_pricename
,如下图所示: How? 怎么样?
Where I'm at 我在哪里
I note that this ER Diagram contains a composite foreign key
, 我注意到该ER图包含一个
composite foreign key
,
I am talking about table
product_pricename
with key of(category_pricename_category_id, category_pricename_pricename_id)
我说的是带有
(category_pricename_category_id, category_pricename_pricename_id)
键的表product_pricename
which is part of another entity's primary key 这是另一个实体的主键的一部分
I am talking about
category_pricename
with PRIMARY KEY of(category_id, pricename_id)
我说的
category_pricename
与PRIMARY KEY(category_id, pricename_id)
Which is totally okay by MySQL. MySQL完全可以。 But when I try to model this in Doctrine I get an error:
但是,当我尝试在Doctrine中对此建模时,出现错误:
[Doctrine\ORM\Mapping\MappingException]
It is not possible to map entity 'CategoryPricename' with a composite
primary key as part of the primary key of another entity
'ProductPricename#categoryPricename'.
I so far have not visualized a proper way to do this in Doctrine. 到目前为止,我还没有在“学说”中想象出执行此操作的正确方法。
I'm thinking I could introduce a surrogate PRIMARY KEY to entities product_pricename
and category_pricename
and re-model my tables. 我在想可以为
product_pricename
和category_pricename
引入一个代理PRIMARY KEY并重新建模我的表。 But would that be a way to do it? 但这是一种方法吗?
Relevant Code 相关代码
class CategoryPricename
{
/**
* @Id @ManyToOne(targetEntity="Category")
* @JoinColumn(name="category_id", referencedColumnName="id", nullable=false)
*
* @var Category
*/
private $category;
/**
* @Id @ManyToOne(targetEntity="Pricename")
* @JoinColumn(name="pricename_id", referencedColumnName="id", nullable=false)
*
* @var Pricename
*/
private $pricename;
}
class ProductPricename
{
/**
* @Id @ManyToOne(targetEntity="Product")
* @JoinColumn(name="product_id", referencedColumnName="id", nullable=false)
*
* @var Product
*/
private $product;
/**
* Note: I shortened the column names for my code
* by removing "category_pricename_" prefix for brevity
* as reflected in JoinColumn's name attributes
*
* @Id
* @ManyToOne(targetEntity="CategoryPricename")
* @JoinColumns({
* @JoinColumn(name="category_id", referencedColumnName="category_id", nullable=false),
* @JoinColumn(name="pricename_id", referencedColumnName="pricename_id", nullable=false)
* })
*
* @var CategoryPricename
*/
private $categoryPricename;
}
Natural vs Surrogate keys 自然键与代理键
It seems like with Doctrine I have to use surrogate keys in my tables to make my design work with Doctrine's limitations. 似乎在使用Doctrine时,我必须在表中使用代理键,以使设计工作不受Doctrine的限制。
OR 要么
I could go with mysqli and use natural keys (as in some tables of the ER Diagram now) 我可以使用mysqli并使用自然键(例如现在在ER图的某些表中)
Part of me goes... which route do I go, surrogate (Doctrine) or natural (mysqli) 我的一部分去了...我走哪条路,替代(Doctrine)还是自然(mysqli)
Modeling of more complex relations in Doctrine requires: 在主义中建模更复杂的关系需要:
id
primary key. id
主键。 For the above to work, you may need change the structure of your tables to where you have no composite primary keys. 为了使上述方法起作用,您可能需要将表的结构更改为没有复合主键的位置。 That is due to Doctrine does allowing composite primary keys to be associated with foreign ids.
这是由于Doctrine确实允许将复合主键与外来ID关联。 It is a limitation.
这是一个限制。 To work with this limitation means having more surrogate keys and less natural keys, which means having more table joins for ie querying
price_history
. 要使用此限制,意味着要具有更多的代理键和更少的自然键,这意味着要具有更多的表联接,例如查询
price_history
。
Alternative way is to use mysqli or PDO. 另一种方法是使用mysqli或PDO。
Alternative way is to not use Doctrine. 另一种方法是不使用学说。 Set up your tables however you need, and use
mysqli
or PDO
. 根据需要设置表,并使用
mysqli
或PDO
。 I chose to go this route because for my design I wanted to use full power of ER Design and DBMS and not have to deal with a lot of table joins. 我之所以选择这条路线,是因为对于我的设计,我想使用ER Design和DBMS的全部功能,而不必处理很多表联接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.