doctrine / orm

Doctrine Object Relational Mapper (ORM)
https://www.doctrine-project.org/projects/orm.html
MIT License
9.94k stars 2.52k forks source link

`OneToMany::indexBy` no longer works when refering a database column #11097

Closed aprat84 closed 5 months ago

aprat84 commented 10 months ago

BC Break Report

Q A
BC Break yes
Version 2.17.0

Summary

Indexing by database column worked, now it doesn't. Better look at previous/current behavior.

Previous behavior

class Language
{
    #[ORM\Id]
    #[ORM\Column(type: 'string', length: 20)]
    private ?string $id = null;
}

class Product {
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer', options: ['unsigned' => true])]
    private ?int $id = null;

    #[ORM\OneToMany(
        mappedBy: 'product',
        targetEntity: ProductTranslation::class,
        cascade: ['persist', 'merge'],
        fetch: 'EAGER',
        indexBy: 'language_id'
    )]
    private Collection $translations;
}

class ProductTranslation {
    #[ORM\Id]
    #[ORM\ManyToOne(targetEntity: Product::class, inversedBy: 'translations')]
    #[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
    private Product $product;

    #[ORM\Id]
    #[ORM\ManyToOne(targetEntity: Language::class)]
    #[ORM\JoinColumn(nullable: false)]
    private Language $language;
}

This worked, indexing Product::$translations by language_id (Language::$id).

Current behavior

I get a warning and an error:

Warning: Undefined array key "language_id"
vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php:846
Error: Call to a member function getValue() on null
vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:3195

How to reproduce

I've created a simple symfony project: https://github.com/aprat84/doctrine-2.17-bug

aprat84 commented 10 months ago

Added a repo with simple project to reproduce the bug: https://github.com/aprat84/doctrine-2.17-bug

igoooor commented 7 months ago

Have you found a solution or a workaround to this? I have the same issue and don't know how to proceed

aprat84 commented 5 months ago

This is fixed by #11380