Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Doctrine Bridge] Class "%s" is not a valid entity or mapped super class - BC Break #47791

Closed
nfragnet opened this issue Oct 5, 2022 · 6 comments
Labels
Bug DoctrineBridge Help wanted Issues and PRs which are looking for volunteers to complete them. Status: Reviewed

Comments

@nfragnet
Copy link

nfragnet commented Oct 5, 2022

Symfony version(s) affected

5.4.x

Description

Hello

The upgrade from Symfony 5.3.x to Symfony 5.4.x led to multiple MappingException in one of my project.

From what I could investigate, I think that this commit could be at the origin of - what I consider being a - BC Break as it changes the default value of mapping type from "annotation" to "attribute" in the following use case.

What I describe below is working properly in Symfony 5.3.x.

How to reproduce

I'm on a legacy project separated into bundles.

One of that bundle provides an embeddable class in an Entity folder.

namespace App\FooBundle\Entity;

/**
 * @ORM\Embeddable
 */
class Foo
{
}

⚠️ It's important to ensure that there's no entity in the App\FooBundle\Entity, only the embeddable.

Then Another bundle contains an entity using that embeddable

namespace App\BarBundle\Entity;

/**
 * @ORM\Entity
 */
class Bar
{
    /**
     * @ORM\Embedded(class="App\FooBundle\Entity\Foo")
     *
     * @var Foo
     */
    protected $foo;
}

and the doctrine configuration is the following:

# config/packages/doctrine.yaml

orm:
    auto_mapping: true
    # "mappings" key is not defined

Possible Solution

The solution I found is to explicitly define the mapping type in doctrine configuration

# config/packages/doctrine.yaml

orm:
  mappings:
    AppFooBundle:
      is_bundle: true
      type: annotation

I don't know if any change has to be done regarding the code itself, maybe it requires an additional row in this part of the upgrade documentation.

Additional Context

No response

@xabbuh
Copy link
Member

xabbuh commented Oct 5, 2022

Can you create a small example application that allows to reproduce your issue?

@nfragnet
Copy link
Author

nfragnet commented Oct 5, 2022

Erf! Will try to do that tomorrow

@nfragnet
Copy link
Author

nfragnet commented Oct 6, 2022

I'm not familiar with reproducers: this is my first one, hope it's ok

https://github.com/nfragnet/symfony_issue_47791

@derrabus
Copy link
Member

derrabus commented Oct 6, 2022

I know this issue. I stumbled accross it myself recently and investigated it already. If a bundle does not ship any entities but only ebeddables, our detection logic fails and DoctrineBundle assumes that the bundle is using attributes for mapping. You have two options:

  • Actually switch that bundle to use atrributes instead of annotations.
  • Manually set the mapping for that bundle to annotations in your doctrine bundle config.

@derrabus derrabus added Status: Reviewed Help wanted Issues and PRs which are looking for volunteers to complete them. and removed Status: Needs Review Status: Waiting feedback labels Oct 6, 2022
@derrabus
Copy link
Member

derrabus commented Oct 6, 2022

I wasn't really sure how to fix it at that time and assumed that the issue would be rare enough to leave it unfixed. But if someone finds a solution, I'll be glad to review and merge it.

@jorissae
Copy link
Contributor

jorissae commented Oct 6, 2022

It's work with something like that

in Symfony\Bridge\Doctrine\DependencyInjection\AbstractDoctrineExtension::detectMappingType() (l316)

if (preg_match('/^ \* @.*'.$quotedMappingObjectName.'\b/m', $content)) {
        $type = 'annotation';
        break;
}

-->

$quotedMappingEmbeddable = preg_quote('Embeddable', '/');
if (
    preg_match('/^ \* @.*'.$quotedMappingObjectName.'\b/m', $content) || 
    preg_match('/^ \* @.*'.$quotedMappingEmbeddable.'\b/m', $content)
) {
        $type = 'annotation';
        break;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug DoctrineBridge Help wanted Issues and PRs which are looking for volunteers to complete them. Status: Reviewed
Projects
None yet
Development

No branches or pull requests

5 participants