我们有一个数据库模式,以简化(略微做作)的形式,看起来像:
在列(domainId、groupId)上设置从用户到域的外键,以保证引用完整性。这种结构可以很好地达到预期目的。
但是,对于与同一个数据库通信的新应用程序,我现在需要为 Doctrine 创建一个映射以映射上述结构,包括两列上的外键关系。
我尝试了以下方法:
<entity name="User" table="users">
<!-- other fields -->
<many-to-one field="domain" target-entity="Domain" fetch="LAZY">
<join-columns>
<join-column name="domainId" referenced-column-name="domainId"/>
<join-column name="groupId" referenced-column-name="groupId"/>
</join-columns>
</many-to-one>
</entity>
但这给了我一个错误:
UnitOfWork.php line 2649: Undefined index: groupId
所以,我的问题是:
在 Doctrine 中描述多列多对一外键关系的正确方法是什么?
为了完整起见,数据库为上面 ERD 中描述的模式创建代码:
CREATE TABLE `users` (
`userId` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`groupId` INT(10) UNSIGNED NOT NULL,
`domainId` INT(10) UNSIGNED NOT NULL,
`someData` VARCHAR(32),
PRIMARY KEY (`userId`),
KEY `key_users_groupId_domainId` (`groupId`, `domainId`)
) ENGINE=InnoDB;
CREATE TABLE `domains` (
`domainId` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`groupId` INT(10) UNSIGNED NOT NULL,
`someOtherData` VARCHAR(32),
PRIMARY KEY (`domainId`),
KEY `key_domains_groupId` (`groupId`)
) ENGINE=InnoDB;
CREATE TABLE `groups` (
`groupId` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`someMoreData` VARCHAR(32),
PRIMARY KEY (`groupId`)
) ENGINE=InnoDB;
ALTER TABLE `users`
ADD CONSTRAINT `fk_users_domains` FOREIGN KEY (`groupId`, `domainId`) REFERENCES `domains` (`groupId`, `domainId`),
ADD CONSTRAINT `fk_users_groups` FOREIGN KEY (`groupId`) REFERENCES `groups` (`groupId`);
ALTER TABLE `domains`
ADD CONSTRAINT `fk_domains_groups` FOREIGN KEY (`groupId`) REFERENCES `groups` (`groupId`);