gpt4 book ai didi

doctrine - Doctrine 2 何时会在使用复合键时出错?

转载 作者:行者123 更新时间:2023-12-03 06:39:55 31 4
gpt4 key购买 nike

我最近正在阅读Doctrine 2's best practices并被此阻止:

25.3. Avoid composite keys
Even though Doctrine fully supports composite keys it is best not to use them if possible. Composite keys require additional work by Doctrine and thus have a higher probability of errors.

我不明白的是:如果“Doctrine完全支持复合键”,那么在处理此类键时怎么会出错?

因此,我想以社区 wiki 的身份提出这个问题,希望它能帮助我们理解“错误概率” 是什么:

您是否有示例可以分享,以展示您遇到的 Doctrine 2 未能很好地/按其应有的方式处理复合键的情况?

最佳答案

就我而言,我相信我已经遇到了一个非常基本的数据库模式的一些问题,该模式包含一个主键由 2 个外键组成的表:

enter image description here

当我生成模型时,不会创建与 product_i18n 实体对应的实体:

$ php doctrine-module.php orm:convert-mapping --namespace="Dbi\Entity\\" \
--from-database --force annotation module/Dbi/src/
Processing entity "Dbi\Entity\Locale"
Processing entity "Dbi\Entity\Product"

$ php doctrine-module.php orm:generate-entities --generate-annotations=1 \
module/Dbi/src
Processing entity "Dbi\Entity\Locale"
Processing entity "Dbi\Entity\Product"

但是,我相信我的database schema已正确创建:

  PRIMARY KEY (`product_id`, `locale_id`) ,
INDEX `fk_product_i18n_locale` (`locale_id` ASC) ,
INDEX `fk_product_i18n_product` (`product_id` ASC) ,
CONSTRAINT `fk_product_i18n_locale`
FOREIGN KEY (`locale_id` )
REFERENCES `mydb`.`locale` (`id` ),
CONSTRAINT `fk_product_i18n_product`
FOREIGN KEY (`product_id` )
REFERENCES `mydb`.`product` (`id` )

此外,Doctrine 2Schema Manager 显示 Doctrine 似乎完美地理解了这种关系(var_dump 输出被更改以使其更加简洁) :删除诸如 arrayRECURSIONstring...):

$em = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default');
$sm = $em->getConnection()->getSchemaManager();
var_dump($sm->->listTables());

object(Doctrine\DBAL\Schema\Table)#290 (10) {
["_name":protected]=> "product_i18n"

["_indexes":protected]=>
["primary"]=>
["_columns":protected]=>
[0]=> "product_id"
[1]=> "locale_id"

["_fkConstraints":protected]=>
["fk_product_i18n_locale"]=>
object(Doctrine\DBAL\Schema\ForeignKeyConstraint)#285 (9) {
["_localColumnNames":protected]=> "locale_id"
["_foreignTableName":protected]=> "locale"
["_foreignColumnNames":protected]=> "id"

["fk_product_i18n_product"]=>
object(Doctrine\DBAL\Schema\ForeignKeyConstraint)#286 (9) {
["_localColumnNames":protected]=> "product_id"
["_foreignTableName":protected]=> "product"
["_foreignColumnNames":protected]=> "id"

因此,我所处的情况是,我可以查询 product_i18n 表的架构,但无法与该表交互,因为未生成相应的实体模型。

更新:正如user1136666指出的:known issues and limitations page颂歌陈述如下:

Although we state that we support composite primary keys that does not currently include foreign keys as primary key columns.

解决方法是定义代理键并在外键上添加唯一约束,如下所示:

CREATE  TABLE IF NOT EXISTS `mydb`.`product_i18n` (
`id` INT NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(45) NULL ,
`description` TEXT NULL ,
`created_at` DATETIME NULL ,
`modified_at` DATETIME NULL ,
`product_id` INT NOT NULL ,
`locale_id` INT NOT NULL ,
INDEX `fk_product_i18n_locale` (`locale_id` ASC) ,
INDEX `fk_product_i18n_product` (`product_id` ASC) ,
PRIMARY KEY (`id`) ,
UNIQUE (`locale_id` , `product_id`),
CONSTRAINT `fk_product_i18n_locale`
FOREIGN KEY (`locale_id` )
REFERENCES `mydb`.`locale` (`id` )
CONSTRAINT `fk_product_i18n_product`
FOREIGN KEY (`product_id` )
REFERENCES `mydb`.`product` (`id` )
ENGINE = InnoDB;

功能请求:http://www.doctrine-project.org/jira/browse/DDC-1926

关于doctrine - Doctrine 2 何时会在使用复合键时出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11427534/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com