Doctrine ORM ManyToMany 缺少字段数据

Doctrine ORM ManyToMany is missing field data

我正在将旧的 PHP7 应用程序升级到 PHP8.1,发现我的 Doctrine 模块(这是 ORM 的 Laminas 集成)有一些奇怪的映射问题。随着升级,我从注释驱动程序切换到属性驱动程序,但由于一个奇怪的原因,我的 ManyToMany 关系给出了一个错误:

我的 class 的子集在这里:

<?php
#[ORM\Entity]
#[ORM\Table(name: 'ScoreScale')]
class ScoreScale
{
    #[ORM\Id]
    #[ORM\GeneratedValue(strategy: 'AUTO')]
    #[ORM\Column(type: 'integer', name: 'scoreScaleId')]
    private $id;

    #[ORM\ManyToMany(targetEntity: Qualification::class, inversedBy: 'scoreScaleCollection')]
    #[ORM\JoinTable(
        name: 'scorescale_qualification',
        joinColumns: [new ORM\JoinColumn(name: 'scoreScaleId', referencedColumnName: 'scoreScaleId')],
        inverseJoinColumns: [new ORM\JoinColumn(name: 'qualificationId', referencedColumnName: 'qualificationId')]
    )]
    private $qualificationCollection;
}
?>

<?php
#[ORM\Entity(repositoryClass: QualificationRepository::class)]
#[ORM\Table(name: 'Qualification')]
class Qualification implements JsonSerializable, Stringable
{
    #[ORM\Id]
    #[ORM\GeneratedValue(strategy: 'AUTO')]
    #[ORM\Column(type: 'integer', name: 'qualificationId')]
    private $id;

    #[ORM\ManyToMany(targetEntity: ScoreScale::class, mappedBy: 'qualificationCollection')]
    private $scoreScaleCollection;
}

实际的问题是ScoreScaleclass上的ManyToMany。我在验证架构完整性时实际遇到的错误是:

 [FAIL] The entity-class Result\Entity\ScoreScale mapping is invalid:
 * The referenced column name 'id' has to be a primary key column on the target entity class 'Result\Entity\ScoreScale'.
 * The referenced column name 'id' has to be a primary key column on the target entity class 'Qualification\Entity\Qualification'.

奇怪的是,我的映射中甚至没有列出参考列 id。当我更深入地研究验证器本身并转储导致此问题的实际数据时,我的映射似乎没有按定义存在。 joinColumns 和 inverseJoinColumns 有什么不同?

Array
(
    [fieldName] => qualificationCollection
    [joinTable] => Array
        (
            [name] => scorescale_qualification
            [schema] => 
            [joinColumns] => Array
                (
                    [0] => Array
                        (
                            [name] => id
                            [referencedColumnName] => id
                            [onDelete] => CASCADE
                        )

                )

            [inverseJoinColumns] => Array
                (
                    [0] => Array
                        (
                            [name] => id
                            [referencedColumnName] => id
                            [onDelete] => CASCADE
                        )

                )

        )

不知何故,我确实认为这可能是映射中的错误,但我不是 100% 确定。希望有人出出主意。

最好的, 皮姆

您在 ScoreScale 实体中用于 JoinTable 的语法似乎是注释和属性的组合。

doctrine/orm 存储库中的 PR 声明如下:

Instead of @JoinTable holding join columns and inverse join columns as a property, you use #[ORM\JoinColumn] and #[ORM\InverseJoinColumn] on the top level.

documentation 显示了一个关于如何为 many-to-many 关系使用属性的示例。

因此对于您的示例,正​​确的语法应该是:

#[ORM\ManyToMany(targetEntity: Qualification::class, inversedBy: "scoreScaleCollection")]
#[ORM\JoinTable(name: "scorescale_qualification")]
#[ORM\JoinColumn(name: "scoreScaleId", referencedColumnName: "scoreScaleId")]
#[ORM\InverseJoinColumn(name: "qualificationId", referencedColumnName: "qualificationId")]
private $qualificationCollection;