Skip to content

Autodetecting mapping type doesn't work for embeddable objects #46410

Closed
@fluffycondor

Description

@fluffycondor

Symfony version(s) affected

5.4, 6.0

Description

Autodetecting mapping type works based on @Entity annotation, but it ignores @Embeddable annotation. In result, Symfony crashes with a
Class App\ValueObject\Foo is not a valid entity or mapped super class
error.

How to reproduce

PHP 8.0+.

  1. Add to config/packages/doctrine.yaml these lines:
App\ValueObject:
       is_bundle: false
       dir: '%kernel.project_dir%/src/ValueObject'
       prefix: 'App\ValueObject'
       alias: App

Docs says annotation is a default value for type, so omitting it.
https://symfony.com/doc/current/reference/configuration/doctrine.html#mapping-configuration

  1. Add a value object to src\ValueObject using an @Embeddable annotation:
<?php

namespace App\ValueObject;

use Doctrine\ORM\Mapping as ORM;

/** @ORM\Embeddable */
class Foo
{
}
  1. Use this value object somewhere in your entities.

  2. Symfony crashes with an error:
    Class App\ValueObject\Foo is not a valid entity or mapped super class
    because it detects annonation type by presence #[.*Entity or * @.*Entity, but not Embeddable.
    And it fallbacks to attribute instead of annonation on PHP8.

Possible Solution

$this->getMappingObjectDefaultName() should return (?:Entity|Embeddable) instead of Entity I guess.
https://github.com/symfony/symfony/blob/6.1/src/Symfony/Bridge/Doctrine/DependencyInjection/AbstractDoctrineExtension.php#L292

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions