简体   繁体   English

当Doctrine不允许我将复合主键与另一个实体的外键关联时,如何为ER图建模?

[英]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关系

  1. product_pricename and category_pricename product_pricenamecategory_pricename
  2. price_history and product_pricename , as in the diagram below: price_historyproduct_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_pricenamecategory_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: 在主义中建模更复杂的关系需要:

  1. Model all relations as an entity. 将所有关系建模为一个实体。 Have each such relation entity have a surrogate id primary key. 让每个这样的关系实体都有一个代理id主键。
  2. Instead of ManyToMany relation use ManyToOne for each side. 代替多对多关系,在每一侧使用多对一。

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 . 根据需要设置表,并使用mysqliPDO 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.

相关问题 原则2:按具有复合主键的实体查找 - Doctrine 2: Find by entity with composite primary key 如何将主键作为外键插入另一个表? - How do I insert the primary key to another table as foreign key? Doctrine - 当主键不等于外键时如何获取记录 - Doctrine - How to get the records when primary key is NOT equal to foreign key 如何使复合外键(不是复合主键)在mysql中唯一 - how to make a composite foreign key (not composite primary key) as unique in mysql 教义多重复合外键 - Doctrine multiple composite foreign key 主义2.2 / Symfony 2.2:使用复合外键持久化实体 - Doctrine 2.2/Symfony 2.2: Persisting an entity with a composite foreign key (Laravel)如何在模型中关联外键的外键 - (Laravel) How to associate foreign key of a foreign key in model Symfony 3 Doctrine Composite Key:实体中的复合主键上不允许使用单个 id - Symfony 3 Doctrine Composite Key: single id is not allowed on composite primary key in entity 如何将主键放入另一个表的外键中? PHP,MySQL - How Do I Grab And Put Primary Key Into Foreign Key In Another Table? PHP, MySQL 如何将数据插入一个表并获取主键作为外键插入另一个表中? - How do I insert data into one table & get the primary key to insert in another table as a foreign key?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM