doctrine / DoctrineBundle

Symfony Bundle for Doctrine ORM and DBAL
https://www.doctrine-project.org/projects/doctrine-bundle.html
MIT License
4.71k stars 451 forks source link

Issue with StaticPHPDriver namespace since doctrine/orm 2.3 (breaks since 3.0) #1793

Closed clementvtrd closed 5 months ago

clementvtrd commented 5 months ago

Context

I'm trying to setup a static php mapping with the following configuration (truncate with unrelated configuration):

doctrine:
    dbal:
    orm:
        mappings:
            Model:
                type: staticphp
                is_bundle: false
                dir: '%kernel.project_dir%/src/Infrastructure/Doctrine/Entity'
                prefix: 'Infrastructure\Doctrine\Entity'

My entity has a loadMetadata method and all should be fine.

Error

However, if I try to run any command that use the default entity manager service, I got the following error:

Symfony\Component\ErrorHandler\Error\ClassNotFoundError^ {#4743
  #message: """
    Attempted to load class "StaticPHPDriver" from namespace "Doctrine\ORM\Mapping\Driver".\n
    Did you forget a "use" statement for "Doctrine\Persistence\Mapping\Driver\StaticPHPDriver"?
    """
  #code: 0
  #file: "./var/cache/dev/ContainerJ7M0HEC/getDoctrine_Orm_DefaultEntityManagerService.php"
  #line: 52

Issue

Digging dipper and it appear that the generated class has this line:

$b->addDriver(new \Doctrine\ORM\Mapping\Driver\StaticPHPDriver([(\dirname(__DIR__, 4).'/src/Infrastructure/Doctrine/Entity')]), 'Infrastructure\\Doctrine\\Entity');

The class \Doctrine\ORM\Mapping\Driver\StaticPHPDriver has been removed from doctrine/orm since v3.0 and deprecated since v2.3.

But the corrected line would be (I guess):

$b->addDriver(new \Doctrine\Persistence\Mapping\Driver\StaticPHPDriver([(\dirname(__DIR__, 4).'/src/Infrastructure/Doctrine/Entity')]), 'Infrastructure\\Doctrine\\Entity');

I tried to patch the generated class and run, for example, doctrine:schema:update --dump-sql and it works.

Proposal

My suggestion would be to patch the file config/orm.xml and replace the value of parameter with key doctrine.orm.metadata.staticphp.class with \Doctrine\Persistence\Mapping\Driver\StaticPHPDriver.

ostrolucky commented 5 months ago

This is not an issue with doctrine-bundle but doctrine-bridge, explained here https://github.com/doctrine/DoctrineBundle/pull/1755#issuecomment-1941555095