在我們的數據庫中,我們有沒有單個自動增加主鍵的表,而是一個複合主鍵,它可能包含或不包含自動增加字段作爲該主要的第一個字段鍵。自動增量的Doctrine 2複合主鍵
例如:
DROP TABLE IF EXISTS `gen_5_23`;
CREATE TABLE IF NOT EXISTS `gen_5_23` (
`id_azienda` int(10) unsigned NULL DEFAULT 1,
`id_sede` int(10) unsigned NULL DEFAULT 1,
`revisione_documento` int(10) unsigned NULL DEFAULT 0,
`premessa_generale` text,
`flag_stampa` char(1) DEFAULT 'N',
PRIMARY KEY (`id_azienda`,`id_sede`,`revisione_documento`),
CONSTRAINT `fk_revisione_documento_gen_5_23` FOREIGN KEY (`revisione_documento`, `id_azienda`, `id_sede`) REFERENCES `agews_revisioni_documenti` (`revisione_documento`, `id_azienda`, `id_sede`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `gen_5_23_consumi`;
CREATE TABLE IF NOT EXISTS `gen_5_23_consumi` (
`id_consumo` int(10) unsigned AUTO_INCREMENT,
`id_azienda` int(10) unsigned NULL DEFAULT 1,
`id_sede` int(10) unsigned NULL DEFAULT 1,
`revisione_documento` int(10) unsigned NULL DEFAULT 0,
`consumo` varchar(255) NULL DEFAULT NULL,
`unita` varchar(255) NULL DEFAULT NULL,
`valore` float(11,2) NULL DEFAULT 0,
PRIMARY KEY (id_consumo,`id_azienda`,`id_sede`,`revisione_documento`),
CONSTRAINT `fk_main_gen_5_23_consumi` FOREIGN KEY (`id_azienda`, `id_sede`, `revisione_documento`) REFERENCES `gen_5_23` (`id_azienda`, `id_sede`, `revisione_documento`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
在gen_5_23_consumi的鍵定義爲是「的原因也有我們的web應用程序,這會盲目服用一排,只改變id_azienda或id_sede或revisione_documento,然後重新插入,通過這種方式,該行將是有效的,因爲如果主鍵僅爲id_consumo,則不會。
我們正在考慮開始使用doctrine 2進行數據庫管理,但是我不明白文檔如何實現這樣一個實體,如果它是可能的話。
你會如何建議做自動增量實現?由於我們的數據庫是MySQL,所以我可以像'SELECT MAX(id_consumo)FROM gen_5_23_consumi'這樣做,然後在結果中加上1,但這仍然不可取,是吧? '因爲在此期間可能會有另一個插入更改最後一個ID .. – 2012-07-26 06:55:31
我更新了答案更清楚。您可以從文檔中查看示例。 D2會關注數據的一致性,因爲每次刷新都在一個事務中。 – WizardZ 2012-07-26 19:38:18
我會按照您的建議檢查文檔,感謝您的建議=) – 2012-07-27 06:52:20