Doctrine composite primary key as part of the primary key of another entity

最后都变了- 提交于 2021-02-05 05:52:08

问题


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.

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'

Does anyone know solution for this situation? It can be Doctrine solution or editing model and database structure.

UPDATE 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;

This is one of entities from database that can't be generated by Doctrine.


回答1:


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.

Solution 1 (preferred):

Add a single, auto-increment primary key to EstablecimientosSec. You can then link to that EstablecimientosSec.id instead.

Solution 2:

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.



来源:https://stackoverflow.com/questions/18766541/doctrine-composite-primary-key-as-part-of-the-primary-key-of-another-entity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!