persistence: Registry::getManagerForClass doesn't work with anonymous classes

Situation:

  • PHP 7.1.5
  • Using windows (it’s an important part)
  • Trying to use Symfony validator to validate anonymous class

The validator point is useless, but what’s triggered behind is the
$registry->getManagerForClass(get_class($myAnonymousClassInstance));

The test

$a = new class {
    public $value;
};

var_dump(get_class($a));

$manager = $this->getDoctrine()->getManagerForClass(get_class($a));

Output on Windows:

class@anonymous\x00E:\dev\www\skeleton\src\AppBundle\Controller\DefaultController.php000001982329027F

With this, I get an ORMException:

Unknown Entity namespace alias 'class@anonymousE'.

Obviously, because there is the : syntax, Doctrine is looking for a specific entity namespace.


I also tested this on linux inside a command and it works:

$a = new class {
    public $value;
};

$manager = $this->getContainer()->get('doctrine')->getManagerForClass(get_class($a));

var_dump($class, $manager);

Output is correct because $manager is null:

"class@anonymous\x00/mnt/e/dev/www/skeleton/src/AppBundle/Command/TestCommand.php0x7f66b1aa5341"
null

The fix

I think the AbstractManagerRegistry::getManagerForClass() should return null if the class name starts with class@anonymous\x00, whatever the platform, because it is the way PHP 7.1 handles anonymous classes names.

What do you think?

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 20 (13 by maintainers)

Most upvoted comments

Can we close this issue now that #286 has been merged?

@MatTheCat I think it will indeed obsolete them.

You should probably ask that the sf issue be reopened, or even send a patch