DoctrineBundle: Can't load entities from bundles using PSR-4

According to PSR-4, the namespace of a class doesn’t have to be a direct mapping to the filesystem. See the first example of the specs

This doesn’t look to be supported because of: https://github.com/doctrine/DoctrineBundle/blob/master/Mapping/DisconnectedMetadataFactory.php#L143

This method tries to remove the namespace from the path, and fails because it can’t find it.

e.g. of a bundle using PSR-4 and VendorName\BundleName as a prefix

$namespace = "VendorName\BundleName\Entity";
$path = "/site/vendor/vendorname/bundlename/Entity";

will fail because there’s no direct mapping

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 30 (11 by maintainers)

Commits related to this issue

Most upvoted comments

@Ocramius why unlikely ? IMO, we should accept the support of generating classes in codebases using PSR-4 if we get a PR for it. Otherwise, lots of users won’t be able to use it properly

I cant update my entites

STOP_RE_GENERATING_ENTITIES_BASED_ON_DB_CHANGES

Here is a fix for PSR-4 compatibility which acts as an optional lookup before the default PSR-1 path generation. It uses the composer autoloader to check for registered PSR-4 prefix paths. If for some reason no composer autoloader is present it continues to generate base paths the old way. It’s therefore fully backwards compatible.

https://gist.github.com/janvennemann/46b2626eee2a4808ed75

The entity and crud controller generators work out of the box with this fix. The path passed to the entity repository generator is still wrong and needs to be fixed, but i don’t need that at the moment so i haven’t done that yet.

What about customize doctrine:generate:entities ?

Instead of rebuild all doctrine with psr-4 support - witch seems complicated -, what about adding an argument to the command in order to activate folders mode :

  • mode with “namespaced folders”
  • mode with “direct folders”

This way each one can select the way it prefer ?

This last comment sounds slightly strange. Please clarify what do you mean.

Why it is not supported? Are there any plans to add PSR-4 support in Doctrine?

I think I can work on adding PSR-4 support to Doctrine. But first of all I should know if this is really needed. Or may be someone is already working on it. Do you think if this is possible?

Thank you for your help!

Thanks @Ocramius for the clarity on why this isn’t a priority. The entity generation is great, but you need to generate them elsewhere and copy them over either way (even with PSR-4 support) to avoid overwriting existing entity files which may have additional logic in them. Adding PSR-4 support might lead people to use this over and over again to keep their entities up to date, which could be problematic.

Hi,

I can’t generate crud because of the base path Is it due to PSR-4?

Thx