教义组合主键作为另一个实体的主键的一部分 [英] Doctrine composite primary key as part of the primary key of another entity
问题描述
我有一个特定的情况,一个实体的组合主键是另一个实体的主键的一部分.这是专业化的情况,但现在不重要了.
I have specific situation where composite primary key of one entity is part of the primary key of another entity. This is case of specialization, but it doesn't matter now.
我使用Doctrine从数据库生成实体,但是Doctrine不支持将复合外键用作主键:
I use Doctrine to generate entities from database, but Doctrine doesn't support composite foreign key as primary key:
It is not possible to map entity 'XXXXX' with a composite primary key as part of the primary key of another entity 'YYYYYY#id_xxxxx'
有人知道这种情况的解决方案吗?它可以是Doctrine解决方案,也可以编辑模型和数据库结构.
Does anyone know solution for this situation? It can be Doctrine solution or editing model and database structure.
更新1
CREATE TABLE `amandman` (
`iddokumenta` int(11) NOT NULL,
`datumdostavljanjaskupstini` date NOT NULL,
`tekst` text,
`datumizmene` date DEFAULT NULL,
`izmenjenitekst` text,
`iddokumentapredlogazakona` int(11) DEFAULT NULL,
`datumdostavljanjaskupstinipredlogazakona` date DEFAULT NULL,
PRIMARY KEY (`iddokumenta`,`datumdostavljanjaskupstini`),
KEY `iddokumentapredlogazakona_idx` (`iddokumentapredlogazakona`,`datumdostavljanjaskupstinipredlogazakona`),
CONSTRAINT `iddokumenta45` FOREIGN KEY (`iddokumenta`, `datumdostavljanjaskupstini`) REFERENCES `dokument` (`iddokument`, `datumdostavljanjaskupstini`) ON DELETE NO ACTION ON UPDATE CASCADE,
CONSTRAINT `iddokumentapredlogazakona` FOREIGN KEY (`iddokumentapredlogazakona`, `datumdostavljanjaskupstinipredlogazakona`) REFERENCES `predlogzakona` (`iddokumenta`, `datumdostavljanjaskupstini`) ON DELETE NO ACTION ON UPDATE CASCADE)
ENGINE=InnoDB DEFAULT CHARSET=utf8;
这是数据库中不能由Doctrine生成的实体之一.
This is one of entities from database that can't be generated by Doctrine.
推荐答案
您正在遇到此问题,因为您的复合外键是另一个表的复合主键.这不是一个好的开发实践,这就是为什么它根本不被Doctrine支持的原因,我对此表示强烈怀疑.
You are running into this problem because your composite foreign key is another table's composite primary key. This is not a good development practice, which is why it is simply not supported by Doctrine, and I strongly doubt that it ever will be.
解决方案1(首选):
向 EstablecimientosSec
添加一个自动递增的主键.然后,您可以改为链接到该 EstablecimientosSec.id
.
Add a single, auto-increment primary key to EstablecimientosSec
. You can then link to that EstablecimientosSec.id
instead.
解决方案2:
如果绝对不可能更改数据库结构,请不要映射关系.相反,您可以使用组合主键在单独的查询中获取相关的 EstablecimientosSec
实体.这不是一个完美的解决方案,但可以在这些约束下工作.提示:避免在循环中查询相关对象.
If changing the database structure is absolutely not possible, do not map the relationship. Instead, you can fetch the related EstablecimientosSec
entities in a separate query using the composite primary key. It's not a prefect solution, but it works under these constraints. Tip: avoid querying the related objects as part of a loop.
这篇关于教义组合主键作为另一个实体的主键的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!