7

I use PHP8, Symfony 5.2 and Doctrine 3.0 in my project, But the PHP 8 attributes, allowed since Doctrine 2.9, doesn't seem to work.

use Doctrine\ORM\Mapping\Entity;

**
* @Entity(repositoryClass="App\Repository\MyClassRepository")
*/
class MyClass
{

works fine.

use Doctrine\ORM\Mapping\Entity;

#[Entity(repositoryClass: MyClassRepository::class)]
class MyClass
{

Return [critical] Uncaught PHP Exception Doctrine\ORM\Mapping\MappingException: "Class "App\Entity\MyClass" is not a valid entity or mapped super class." at .../vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/MappingException.php line 378

here is my composer.json :

"composer/package-versions-deprecated": "1.11.99.1",
"doctrine/doctrine-bundle": "^2.3",
"doctrine/doctrine-migrations-bundle": "^3.1",
"doctrine/orm": "^3.0",
"symfony/console": "5.2.*",
"symfony/dotenv": "5.2.*",
"symfony/flex": "^1.3.1",
"symfony/framework-bundle": "5.2.*",
"symfony/proxy-manager-bridge": "5.2.*",
"symfony/yaml": "5.2.*"
5
  • 1
    Maybe because 3.0 has not been released yet? I would imagine the doctrine bundle would need at least a few tweaks.
    – Cerad
    Apr 16, 2021 at 15:18
  • 1
    Have you imported MyClassRepository?
    – miken32
    Apr 16, 2021 at 15:35
  • @Cerad, yeah, I think you're right. Probably the doctrine bundle needs a few adjustments. Thanks. Apr 19, 2021 at 6:56
  • @miken32, what do you mean by imported ? Apr 19, 2021 at 6:57
  • 1
    PHP 8 attributes for Doctrine ORM Symfony configuration was not fully supported until doctrine/doctrine-bundle 2.4+. ^2.3 should install it, but it may have been locked prior. The Symfony doctrine mapping type declaration config still defaults to annotation.
    – Will B.
    Sep 20, 2021 at 20:40

2 Answers 2

5

This is because of doctrine bundle configuration. If all entities within the bundle use attributes just switch the metadata driver from "annotation" to "attribute"

doctrine:
    orm:
        auto_generate_proxy_classes: true
        entity_managers:
            default:
                ...
                mappings:
                    MyBundle:
                        type: attribute

If some entities within a bundles use attributes and some others annotations - than it is better either choose only one format for metadata or implement a custom metadata driver.

1
  • 2
    Here is the Symfony documentation for any who are wanting, Doctrine Config Reference. It is important to note the documentation for PHP 8 attributes support was added in Symfony 5.3+
    – Will B.
    Sep 20, 2021 at 20:16
2

The solution is use a custom AnnotationDriver.

Sample fully works implementation: https://github.com/baraja-core/doctrine/blob/master/src/Orm/Mapping/AnnotationDriver.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.