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

Issue with doctrine/orm 2.12.x #1090

Closed
michnovka opened this issue Apr 11, 2022 · 31 comments · Fixed by #1098
Closed

Issue with doctrine/orm 2.12.x #1090

michnovka opened this issue Apr 11, 2022 · 31 comments · Fixed by #1098
Labels
Bug Bug Fix Status: Needs Review Needs to be reviewed

Comments

@michnovka
Copy link

Symfony version(s) affected

5.4.7

Description

Symfony make entity command results in Exception

symfony console make:entity

[critical] Error thrown while running command "make:entity". Message: "Cannot access protected property Doctrine\ORM\Mapping\Driver\AttributeDriver::$classNames"

How to reproduce

  1. Set up new Symfony project
  2. Use doctrine/orm 2.12.x-dev package
  3. run make:entity

Possible Solution

No response

Additional Context

No response

@michnovka michnovka added the Bug Bug Fix label Apr 11, 2022
@carsonbot carsonbot added the Status: Needs Review Needs to be reviewed label Apr 11, 2022
@stof stof transferred this issue from symfony/symfony Apr 11, 2022
@stof
Copy link
Member

stof commented Apr 11, 2022

I transferred the issue to MakerBundle, which is the proper place to report that.

@michnovka
Copy link
Author

Doctrine ORM 2.12.x is released and this issue persists.

@michnovka
Copy link
Author

The issue stems from

$classNames = (new \ReflectionClass(AnnotationDriver::class))->getProperty('classNames');
$classNames->setAccessible(true);
// Invalidating the cached AnnotationDriver::$classNames to find new Entity classes
foreach ($this->mappingDriversByPrefix ?? [] as $managerName => $prefixes) {
foreach ($prefixes as [$prefix, $annotationDriver]) {
if (null !== $annotationDriver) {
$classNames->setValue($annotationDriver, null);

I am not sure whats going on here. The ReflectionClass uses Doctrine\Persistence\Mapping\Driver\AnnotationDriver But in my case DoctrineHelper::mappingDriversByPrefix contains instance of Doctrine\ORM\Mapping\Driver\AttributeDriver.

So we are mixing types in here. Why one is from ORM, one is from Persistance?

the fix for my specific case is to simply swap

$classNames = (new \ReflectionClass(AnnotationDriver::class))->getProperty('classNames');

for

$classNames = (new \ReflectionClass(\Doctrine\ORM\Mapping\Driver\AttributeDriver::class))->getProperty('classNames');

But I doubt this will be the generic solution we are looking for.

@michnovka
Copy link
Author

FYI there was Doctrine/Persistance 3.0 released, which might be the real cause of the issue, since doctrine/orm 2.12.x uses it

@RikudouSage
Copy link

Any workaround for this?

@kostyan-org
Copy link

found the same problem today

"symfony/maker-bundle": "^1.38"
"doctrine/orm": "^2.12"
"doctrine/doctrine-bundle": "^2.6"

symfony console make:entity

In DoctrineHelper.php line 187:
Cannot access protected property Doctrine\ORM\Mapping\Driver\AnnotationDriver::$classNames

@kostyan-org
Copy link

"symfony/maker-bundle": "1.28.0"
version without bug

@Bechir-Marco
Copy link

I have just solved this issue by modifying "doctrine/annotations "version in composer.json after checking which versions are allowed in composer.lock "conflict"

@lhapaipai
Copy link

I have just solved this issue by modifying "doctrine/annotations "version in composer.json after checking which versions are allowed in composer.lock "conflict"

Hi @Bechir-Marco ,
thanks for your feedback, can you show us your composer.json file ?

@Bechir-Marco
Copy link

Bechir-Marco commented Apr 20, 2022

    {
    "type": "project",
    "license": "proprietary",
    "require": {
        "php": ">=7.4",
        "ext-ctype": "*",
        "ext-iconv": "*",
        "doctrine/annotations": "1.12.1",
        "doctrine/doctrine-bundle": "^2.2",
        "doctrine/doctrine-migrations-bundle": "^3.0",
        "doctrine/orm": "2.8",
        "gesdinet/jwt-refresh-token-bundle": "^1.1",
        "phpdocumentor/reflection-docblock": "^5.3",
        "sensio/framework-extra-bundle": "^6.2",
        "stof/doctrine-extensions-bundle": "^1.7",
        "symfony/asset": "4.4.*",
        "symfony/console": "4.4.*",
        "symfony/dotenv": "4.4.*",
        "symfony/expression-language": "4.4.*",
        "symfony/flex": "^1.3.1",
        "symfony/form": "4.4.*",
        "symfony/framework-bundle": "4.4.*",
        "symfony/http-client": "4.4.*",
        "symfony/intl": "4.4.*",
        "symfony/mailer": "4.4.*",
        "symfony/monolog-bundle": "^3.1",
        "symfony/process": "4.4.*",
        "symfony/property-access": "4.4.*",
        "symfony/property-info": "4.4.*",
        "symfony/proxy-manager-bridge": "4.4.*",
        "symfony/security-bundle": "4.4",
        "symfony/serializer": "4.4.*",
        "symfony/translation": "4.4.*",
        "symfony/twig-bundle": "4.4.*",
        "symfony/validator": "4.4.*",
        "symfony/web-link": "4.4.*",
        "symfony/web-server-bundle": "4.4.*",
        "symfony/yaml": "4.4.*",
        "symfonycasts/verify-email-bundle": "^1.10",
        "twig/extra-bundle": "^2.12|^3.0",
        "twig/twig": "^2.12|^3.0",
        "vich/uploader-bundle": "1.19.0"
    },
    "require-dev": {
        "phpunit/phpunit": "^9.5",
        "symfony/browser-kit": "4.4.*",
        "symfony/css-selector": "4.4.*",
        "symfony/debug-bundle": "4.4.*",
        "symfony/maker-bundle": "v1.30.1",
        "symfony/phpunit-bridge": "^6.0",
        "symfony/stopwatch": "4.4.*",
        "symfony/web-profiler-bundle": "4.4.*"
    },
    "config": {
        "allow-plugins": {
            "composer/package-versions-deprecated": true,
            "symfony/flex": true
        },
        "preferred-install": {
            "*": "dist"
        },
        "sort-packages": true
    },
    "autoload": {
        "psr-4": {
            "App\\": "src/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "App\\Tests\\": "tests/"
        }
    },
    "replace": {
        "paragonie/random_compat": "2.*",
        "symfony/polyfill-ctype": "*",
        "symfony/polyfill-iconv": "*",
        "symfony/polyfill-php71": "*",
        "symfony/polyfill-php70": "*",
        "symfony/polyfill-php56": "*"
    },
    "scripts": {
        "auto-scripts": {
            "cache:clear": "symfony-cmd",
            "assets:install %PUBLIC_DIR%": "symfony-cmd"
        },
        "post-install-cmd": [
            "@auto-scripts"
        ],
        "post-update-cmd": [
            "@auto-scripts"
        ]
    },
    "conflict": {
        "symfony/symfony": "*"
    },
    "extra": {
        "symfony": {
            "allow-contrib": false,
            "require": "4.4.*"
        }
    }
}

@snipershady
Copy link

found the same problem today

"symfony/maker-bundle": "^1.38" "doctrine/orm": "^2.12" "doctrine/doctrine-bundle": "^2.6"

symfony console make:entity

In DoctrineHelper.php line 187: Cannot access protected property Doctrine\ORM\Mapping\Driver\AnnotationDriver::$classNames

same problem on a brand new project symfony 6.0

@MatuxGG
Copy link

MatuxGG commented Apr 20, 2022

Same issue

@lhapaipai
Copy link

lhapaipai commented Apr 20, 2022

Thank you @Bechir-Marco ,
Unfortunatly it doesn't solve my issue.

{
    "type": "project",
    "license": "proprietary",
    "minimum-stability": "stable",
    "prefer-stable": true,
    "require": {
        "php": ">=8.0.2",
        "ext-ctype": "*",
        "ext-iconv": "*",
        "doctrine/doctrine-bundle": "^2.5",
        "doctrine/doctrine-migrations-bundle": "^3.2",
        "doctrine/orm": "^2.11",
        "symfony/console": "6.0.*",
        "symfony/dotenv": "6.0.*",
        "symfony/flex": "^2",
        "symfony/framework-bundle": "6.0.*",
        "symfony/proxy-manager-bridge": "6.0.*",
        "symfony/runtime": "6.0.*",
        "symfony/yaml": "6.0.*"
    },
    "config": {
        "allow-plugins": {
            "composer/package-versions-deprecated": true,
            "symfony/flex": true,
            "symfony/runtime": true
        },
        "optimize-autoloader": true,
        "preferred-install": {
            "*": "dist"
        },
        "sort-packages": true,
        "platform": {
            "php": "8.1"
        }
    },

    "conflict": {
        "symfony/symfony": "*"
    },
    "extra": {
        "symfony": {
            "allow-contrib": false,
            "require": "6.0.*"
        }
    },
    "require-dev": {
        "symfony/maker-bundle": "^1.38",
        "symfony/web-profiler-bundle": "6.0.*"
    }
}

@czachor
Copy link

czachor commented Apr 20, 2022

Temporary workaround

Composer.json - change:

"doctrine/orm": "^2.12",

to:

"doctrine/orm": "^2.11",

add (to prevent 2.12 being installed):

"conflict": {
  "symfony/symfony": "*",
  "doctrine/orm": "2.12.0"
},

@Amand04
Copy link

Amand04 commented Apr 20, 2022

Même problème que Tireur d'Elite.
J'ai suivi les indications de czachor, j'ai modifié le fichier composer.json.
J'ai réussi quand même à créer mon entité et son repository en donnant en nom à l'entité dans l'instruction. Il m'a quand même indiqué:

Dans DoctrineHelper.php ligne 187 : Impossible d'accéder à la propriété protégée Doctrine\ORM\Mapping\Driver\AnnotationDriver::$classNames

mais l'a crée malgrés tout...?!

@Th3BiSh0p
Copy link

Même problème que Tireur d'Elite. J'ai suivi les indications de czachor, j'ai modifié le fichier composer.json. J'ai réussi quand même à créer mon entité et son repository en donnant en nom à l'entité dans l'instruction. Il m'a quand même indiqué:

Dans DoctrineHelper.php ligne 187 : Impossible d'accéder à la propriété protégée Doctrine\ORM\Mapping\Driver\AnnotationDriver::$classNames

mais l'a crée malgrés tout...?!

tu as fait un composer update après la modification du composer.json ? Pour moi ça a bien contourné le problème.

@JB-oclock
Copy link
Contributor

JB-oclock commented Apr 20, 2022

bin/console make:entity Car
 created: src/Entity/Car.php
 created: src/Repository/CarRepository.php

In DoctrineHelper.php line 187:
                                                                                              
  Cannot access protected property Doctrine\ORM\Mapping\Driver\AnnotationDriver::$classNames  
                                                                                              

But if you don't provide entity name, it can't ask you question, so it doesn't create anything

stack trace of the actual error

Error {#255
  #message: "Cannot access protected property Doctrine\ORM\Mapping\Driver\AnnotationDriver::$classNames"
  #code: 0
  #file: "./vendor/symfony/maker-bundle/src/Doctrine/DoctrineHelper.php"
  #line: 187
  trace: {
    ./vendor/symfony/maker-bundle/src/Doctrine/DoctrineHelper.php:189 { …}
    ./vendor/symfony/maker-bundle/src/Doctrine/DoctrineHelper.php:163 { …}
    ./vendor/symfony/maker-bundle/src/Maker/MakeEntity.php:515 { …}
    ./vendor/symfony/maker-bundle/src/Maker/MakeEntity.php:116 { …}
    ./vendor/symfony/maker-bundle/src/Command/MakerCommand.php:99 { …}
    ./vendor/symfony/console/Command/Command.php:283 { …}
    ./vendor/symfony/console/Application.php:1033 { …}
    ./vendor/symfony/framework-bundle/Console/Application.php:96 { …}
    ./vendor/symfony/console/Application.php:299 { …}
    ./vendor/symfony/framework-bundle/Console/Application.php:82 { …}
    ./vendor/symfony/console/Application.php:171 { …}
    ./vendor/symfony/runtime/Runner/Symfony/ConsoleApplicationRunner.php:54 { …}
    ./vendor/autoload_runtime.php:35 { …}
    ./bin/console:11 {
      › 
      › require_once dirname(__DIR__).'/vendor/autoload_runtime.php';
// ./vendor/symfony/maker-bundle/src/Doctrine/DoctrineHelper.php
public function getEntitiesForAutocomplete(): array
{
        $entities = [];

        if ($this->isDoctrineInstalled()) {
            $allMetadata = $this->getMetadata(); // will generate the error

@JB-oclock
Copy link
Contributor

adding a try-catch on line 187 get rid of the message, Entities AutoComplete still works in console.

try{
    $classNames->setValue($annotationDriver, null);
} catch(\Throwable $th){}

$classNames = (new \ReflectionClass(AnnotationDriver::class))->getProperty('classNames');
$classNames->setAccessible(true);
// Invalidating the cached AnnotationDriver::$classNames to find new Entity classes
foreach ($this->mappingDriversByPrefix ?? [] as $managerName => $prefixes) {
foreach ($prefixes as [$prefix, $annotationDriver]) {
if (null !== $annotationDriver) {
$classNames->setValue($annotationDriver, null);
}
}
}

I don't understand, on line 181 we set $classNames accessible as a \ReflectionProperty, but when setting a value on line 187 it can't access protected property.

@mbabker
Copy link

mbabker commented Apr 20, 2022

See https://github.com/doctrine/orm/blob/2.12.x/UPGRADE.md#bc-break-attributedriver-and-annotationdriver-no-longer-extends-parent-class-from-doctrinepersistence

The $classNames property being reflected is on a parent class that is no longer inherited from. This crude patch should help make things work while someone makes a "better" fix (I generally don't use this bundle, so I can't say if it's any good):

diff --git a/src/Doctrine/DoctrineHelper.php b/src/Doctrine/DoctrineHelper.php
index 8c05db2..48cdad6 100644
--- a/src/Doctrine/DoctrineHelper.php
+++ b/src/Doctrine/DoctrineHelper.php
@@ -177,14 +177,17 @@ final class DoctrineHelper
      */
     public function getMetadata(string $classOrNamespace = null, bool $disconnected = false)
     {
-        $classNames = (new \ReflectionClass(AnnotationDriver::class))->getProperty('classNames');
-        $classNames->setAccessible(true);
-
         // Invalidating the cached AnnotationDriver::$classNames to find new Entity classes
         foreach ($this->mappingDriversByPrefix ?? [] as $managerName => $prefixes) {
             foreach ($prefixes as [$prefix, $annotationDriver]) {
                 if (null !== $annotationDriver) {
-                    $classNames->setValue($annotationDriver, null);
+                    $refl = new \ReflectionClass($annotationDriver);
+
+                    if ($refl->hasProperty('classNames')) {
+                        $classNames = (new \ReflectionClass($annotationDriver))->getProperty('classNames');
+                        $classNames->setAccessible(true);
+                        $classNames->setValue($annotationDriver, null);
+                    }
                 }
             }
         }
@@ -218,7 +221,11 @@ final class DoctrineHelper
                     $metadataDriver = $em->getConfiguration()->getMetadataDriverImpl();
                     if ($this->isInstanceOf($metadataDriver, MappingDriverChain::class)) {
                         foreach ($metadataDriver->getDrivers() as $driver) {
-                            if ($this->isInstanceOf($driver, AnnotationDriver::class)) {
+                            $refl = new \ReflectionClass($driver);
+
+                            if ($refl->hasProperty('classNames')) {
+                                $classNames = (new \ReflectionClass($driver))->getProperty('classNames');
+                                $classNames->setAccessible(true);
                                 $classNames->setValue($driver, null);
                             }
                         }

@lionelkimbs
Copy link

This worked for me :

  1. Edit composer.json to change doctrine/orm version
...
    "require": {
        ...
        "doctrine/orm": "^2.11",
        ...
    }
    ...
    "conflict": {
        "symfony/symfony": "*",
        "doctrine/orm": "2.12.0"
    },
...
  1. Update dependencies : composer update
  2. Enjoy !

@kacperjcb
Copy link

This worked for me:
composer require doctrine/orm:2.11

@sinE800
Copy link

sinE800 commented Apr 21, 2022

Temporary workaround

Composer.json - change:

"doctrine/orm": "^2.12",

to:

"doctrine/orm": "^2.11",

add (to prevent 2.12 being installed):

"conflict": {
  "symfony/symfony": "*",
  "doctrine/orm": "2.12.0"
},

Merci/thanks.
(I hope they fix the 2.12 version)

@Amand04
Copy link

Amand04 commented Apr 21, 2022

Même problème que Tireur d'Elite. J'ai suivi les indications de czachor, j'ai modifié le fichier composer.json. J'ai réussi quand même à créer mon entité et son référentiel en donnant en nom à l'entité dans l'instruction. Il m'a quand même indiqué :
Dans DoctrineHelper.php ligne 187 : Impossible d'accéder à la propriété protégée Doctrine\ORM\Mapping\Driver\AnnotationDriver::$classNames
mais l'a crée malgrés tout...?!

tu as fait un composer update après la modification du composer.json ? Pour moi ça a bien contourné le problème.

Oui cela fonctionne. Merci pour l'indication je suis une grande débutante...

@snipershady
Copy link

downgrade works fine, but I hope doctrine developers will release a new version soon.
It's really unpleasant to run a brand new project and receive an exception from a "core" package.

@tourze
Copy link

tourze commented Apr 21, 2022

image

same problem

@stof
Copy link
Member

stof commented Apr 21, 2022

Well, the issue is not in doctrine but in MakerBundle. When you use Reflection to access internal parts of a class, you cannot expect it to be covered by BC.

@snipershady
Copy link

snipershady commented Apr 22, 2022

maker bundle was updated yesterday

v1.39.0 2022-04-21 18:16 UTC

but it still doesn't works !!!
@fabpot @nicolas-grekas
I've forwarded this issue to doctrine/orm here: doctrine/orm#9669
but reading the answer from greg0ire
doctrine/orm#9669 (comment)
looks like there is no communication between Symfony staff and doctrine maintainers

php bin/console make:entity --verbose

In DoctrineHelper.php line 187:
                                                                                              
  [Error]                                                                                     
  Cannot access protected property Doctrine\ORM\Mapping\Driver\AnnotationDriver::$classNames  

Exception trace:
  at /home/shady/public_html/MY_NEW_PROJECT/vendor/symfony/maker-bundle/src/Doctrine/DoctrineHelper.php:187
 ReflectionProperty->setValue() at /home/shady/public_html/MY_NEW_PROJECT/vendor/symfony/maker-bundle/src/Doctrine/DoctrineHelper.php:187
 Symfony\Bundle\MakerBundle\Doctrine\DoctrineHelper->getMetadata() at /home/shady/public_html/MY_NEW_PROJECT/vendor/symfony/maker-bundle/src/Doctrine/DoctrineHelper.php:162
 Symfony\Bundle\MakerBundle\Doctrine\DoctrineHelper->getEntitiesForAutocomplete() at /home/shady/public_html/MY_NEW_PROJECT/vendor/symfony/maker-bundle/src/Maker/MakeEntity.php:515
 Symfony\Bundle\MakerBundle\Maker\MakeEntity->createEntityClassQuestion() at /home/shady/public_html/MY_NEW_PROJECT/vendor/symfony/maker-bundle/src/Maker/MakeEntity.php:116
 Symfony\Bundle\MakerBundle\Maker\MakeEntity->interact() at /home/shady/public_html/MY_NEW_PROJECT/vendor/symfony/maker-bundle/src/Command/MakerCommand.php:99
 Symfony\Bundle\MakerBundle\Command\MakerCommand->interact() at /home/shady/public_html/MY_NEW_PROJECT/vendor/symfony/console/Command/Command.php:276
 Symfony\Component\Console\Command\Command->run() at /home/shady/public_html/MY_NEW_PROJECT/vendor/symfony/console/Application.php:1007
 Symfony\Component\Console\Application->doRunCommand() at /home/shady/public_html/MY_NEW_PROJECT/vendor/symfony/framework-bundle/Console/Application.php:94
 Symfony\Bundle\FrameworkBundle\Console\Application->doRunCommand() at /home/shady/public_html/MY_NEW_PROJECT/vendor/symfony/console/Application.php:299
 Symfony\Component\Console\Application->doRun() at /home/shady/public_html/MY_NEW_PROJECT/vendor/symfony/framework-bundle/Console/Application.php:80
 Symfony\Bundle\FrameworkBundle\Console\Application->doRun() at /home/shady/public_html/MY_NEW_PROJECT/vendor/symfony/console/Application.php:171
 Symfony\Component\Console\Application->run() at /home/shady/public_html/MY_NEW_PROJECT/vendor/symfony/runtime/Runner/Symfony/ConsoleApplicationRunner.php:54
 Symfony\Component\Runtime\Runner\Symfony\ConsoleApplicationRunner->run() at /home/shady/public_html/MY_NEW_PROJECT/vendor/autoload_runtime.php:29
 require_once() at /home/shady/public_html/MY_NEW_PROJECT/bin/console:11

make:entity [-a|--api-resource] [-b|--broadcast] [--regenerate] [--overwrite] [--] [<name>]

@kevinblewusi
Copy link

This worked for me :

  1. Edit composer.json to change doctrine/orm version
...
    "require": {
        ...
        "doctrine/orm": "^2.11",
        ...
    }
    ...
    "conflict": {
        "symfony/symfony": "*",
        "doctrine/orm": "2.12.0"
    },
...
  1. Update dependencies : composer update
  2. Enjoy !

Thanks, this work fine for me !

@stof
Copy link
Member

stof commented Apr 22, 2022

Well, the release of MakerBundle of yesterday fixes other things, but not that bug. Nobody has opened a PR with a fix yet.

@jrushlow
Copy link
Collaborator

jrushlow commented Apr 22, 2022

A fix is in the works for this in #1098 or possibly a subsequent PR

@sarim
Copy link

sarim commented Apr 22, 2022

While a proper fix is being worked on, maybe e hotfix release could be released with makerbundle's composer.json adding a conflict with doctrine/orm": "2.12.0"

Tofandel added a commit to Tofandel/maker-bundle that referenced this issue Apr 22, 2022
Tofandel added a commit to Tofandel/maker-bundle that referenced this issue Apr 22, 2022
@Tofandel Tofandel mentioned this issue Apr 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Bug Fix Status: Needs Review Needs to be reviewed
Projects
None yet