Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Symfony maker: Cannot find the entity manager for class

ErrorCannot find the entity manager for class "App\Entity\User". Ensure entity uses attribute mapping.

Short answer

MakerBundle asks Doctrine which entity manager owns a class. This error means no manager's mapping covers that namespace, or the mapping is not the attribute type MakerBundle expects. Check config/packages/doctrine.yaml: the mapping needs type attribute, a dir that holds the class and a prefix that matches its namespace.

  • Applies to Symfony 7, DoctrineBundle 2, MakerBundle 1
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Read the mappings in your Doctrine config and compare each prefix and dir with the namespace line and folder of the class named in the error.

    cat config/packages/doctrine.yaml
    head -5 src/Entity/User.php
  2. 2Match what you see, then jump to its fix

    1. 1The mapping shows type annotation or xml, while the entities use PHP attributes.The mapping type is not attributeFix
    2. 2The class is in App\Model or another namespace, and no mapping has that prefix.The class namespace does not match any mapping prefixFix
    3. 3doctrine.yaml defines entity_managers (default and another) with their own mappings.With several entity managers, the class is not mapped by the manager you expectFix

What does the error look like?

  • Cannot find the entity manager for class "App\Entity\User". Ensure entity uses attribute mapping.

Cause 1 of 3

The mapping type is not attribute

1Check

The exception message itself says to ensure the entity uses attribute mapping. DoctrineBundle's valid types are attribute, xml, php and staticphp.

grep -n -B2 -A6 "mappings" config/packages/doctrine.yaml

2Fix

Set the mapping type to attribute.

doctrine:
    orm:
        mappings:
            App:
                type: attribute
                dir: '%kernel.project_dir%/src/Entity'
                prefix: 'App\Entity'
                is_bundle: false

3Verify

Clear the cache and run the maker command again.

php bin/console cache:clear
php bin/console make:entity Product

Confirmed by symfony.com

Did this fix it?

Cause 2 of 3

The class namespace does not match any mapping prefix

1Check

A class is only found by a manager whose mapping prefix starts the class namespace and whose dir holds the file. Compare the namespace line in the class with prefix in doctrine.yaml.

head -5 src/Entity/User.php
grep -n "prefix\|dir:" config/packages/doctrine.yaml

2Fix

Move the class under the mapped namespace, or add a mapping whose dir and prefix point to where it lives.

doctrine:
    orm:
        mappings:
            App:
                type: attribute
                dir: '%kernel.project_dir%/src/Entity'
                prefix: 'App\Entity'
                is_bundle: false

3Verify

Clear the cache and run the failing maker command again; it no longer raises the error.

php bin/console cache:clear
php bin/console make:entity User

Confirmed by symfony.com

Did this fix it?

Cause 3 of 3

With several entity managers, the class is not mapped by the manager you expect

1Check

Each entity manager only manages the entities in its own mappings dir and prefix. A class outside all of them has no manager.

grep -n -A25 "entity_managers" config/packages/doctrine.yaml

2Fix

Add the class's directory and prefix to the mapping of the manager that should own it, and fetch that manager by name.

doctrine:
    orm:
        default_entity_manager: default
        entity_managers:
            default:
                connection: default
                mappings:
                    Main:
                        is_bundle: false
                        type: attribute
                        dir: '%kernel.project_dir%/src/Entity/Main'
                        prefix: 'App\Entity\Main'

3Verify

Clear the cache and run the failing maker command again.

php bin/console cache:clear
php bin/console make:entity Main\\Product

Confirmed by symfony.com

Did this fix it?

How do I stop it coming back?

  • Keep entities in the mapped folder and namespace (src/Entity and App\Entity by default).
  • Use attribute mapping for new code and keep type: attribute on every mapping.
  • After editing doctrine.yaml, clear the cache before running make commands.
  • With multiple entity managers, give each its own folder and prefix and avoid overlaps.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Related fixes

Back end

make:migration says the metadata storage is not up to date

The metadata storage is not up to date, please run the sync-metadata-storage command to fix this issue.

Doctrine Migrations keeps its history in a table and refuses to work if it thinks that table has an old layout.

3 causes, each with a fix

Back end

ng serve says it cannot determine the project or target

Cannot determine project for command.

The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.