php-具有自动增量的Doctrine 2复合主键
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php-具有自动增量的Doctrine 2复合主键,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2650字,纯文字阅读大概需要4分钟。
内容图文
在我们的数据库中,我们有一些表,这些表没有单个自动递增主键,而是一个复合表,该组合可能包含也可能不包含一个自动递增字段作为该主键的第一个字段.
例如:
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或versione_documento然后重新插入它,因此该行将是有效的,因为它不会如果主键只是id_consumo.
我们正在考虑开始使用准则2进行数据库管理,但是从文档中我无法理解,如果可能的话,您将如何实现这样的实体.
解决方法:
可能但并非开箱即用.您可以将复合键作为实体的主键:http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/tutorials/composite-primary-keys.html,但是由于以下原因,应在代码中执行自动增量实现:
Every entity with a composite key cannot use an id generator other
than “ASSIGNED”. That means the ID fields have to have their values
set before you call EntityManager#persist($entity).
要模拟自动增量行为,可以将id为自动增量PK的序列表并为该表创建一个实体.将主要实体之间的关系添加到代表自动递增序列的实体,请参见上页的ArticleArticle类,您的代码应非常相似:
Class Gen523consumiAuto
{
/** @Id @Column(type="integer") @GeneratedValue */
private $id;
}
Class Gen523consumi
{
/** @Id @OneToOne(targetEntity="Gen523consumiAuto") */
private $idConsumo;
/** @Id @Column(type="integer") */
private $idAzienda;
/** @Id @Column(type="integer") */
private $idSede;
/** @Id @Column(type="integer") */
private $revisioneDocumento;
}
内容总结
以上是互联网集市为您收集整理的php-具有自动增量的Doctrine 2复合主键全部内容,希望文章能够帮你解决php-具有自动增量的Doctrine 2复合主键所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。