Skip to content

Fail to load entity that has enum as primary key via ManyToOne association #10471

@pauljura

Description

@pauljura

BC Break Report

Q A
BC Break yes
Version 2.14.1

Summary

I have an entity "Field" that has a ManyToOne relationship to "FieldType". The ID column of FieldType is backed by a native PHP enum FieldTypeID.

I can load a FieldType entity just fine, but if I load a Field and try to access the FieldType from it, I get an error.

Previous behavior

This was working fine in 2.14.0

Current behavior

Exception is thrown:

Object of class App\Entity\Enum\FieldTypeID could not be converted to string
File: vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php
Line: 1575

How to reproduce

class Field
{
    #[ORM\Id]
    #[ORM\GeneratedValue(strategy: 'IDENTITY')]
    #[ORM\Column(name: 'id', type: Types::INTEGER, nullable: false)]
    private ?int $id = null;

    #[ORM\ManyToOne(targetEntity: FieldType::class)]
    #[ORM\JoinColumn(name: 'type_id', referencedColumnName: 'id', nullable: false)]
    private FieldType $type;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getType(): ?FieldType
    {
        return $this->type;
    }
}

class FieldType
{
    #[ORM\Id]
    #[ORM\GeneratedValue(strategy: 'NONE')]
    #[ORM\Column(name: 'id', type: Types::INTEGER, nullable: false, enumType: FieldTypeID::class)]
    private readonly FieldTypeID $id;

    public function getId(): int
    {
        return $this->id->value;
    }
}

enum FieldTypeID: int
{
    case FOO = 1;
    case BAR = 2;
    case BAZ = 3;
}

// accessing FieldType directly works OK
$fieldType = $doctrine->getRepository(FieldType::class)->find(1);
$id = $fieldType->getId();

// accessing via association fails
$field = $doctrine->getRepository(Field::class)->find(1);
$fieldType = $field->getType();
$id = $fieldType->getId(); // exception thrown here

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions