Unable to find the object manager associated with

2019-02-27 20:59发布

问题:

Using Symfony 2.7, I am trying to create a user registration form. I've used Symfony's own guide for this, and the form is rendered and fillable.

When I fill in the form, and press "Register", I get the following error.

Unable to find the object manager associated with an entity of class "UserBundle\Models\User"

This is triggered on the following condition in vendor/symfony/symfony/src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntityValidator.php at line 75

$em = $this->registry->getManagerForClass(get_class($entity));
        if (!$em) {
            throw new ConstraintDefinitionException(sprintf('Unable to find the object manager associated with an entity of class "%s".', get_class($entity)));
        }

In the createAction in my UserController, the Entity Manager is fetched by $em = $this->getDoctrine()->getManager(); as per the instructions in the Symfony Cookbook.

I can't find out what's going here. Any help appreciated!

Also, please let me know if there is some information missing that I should have provided.

回答1:

I think unless you specifically map a directory in your config, doctrine is going to look in YourBundle/Entity - this is where your User.php example diverges from the cookbook recipe.

If you want to change this you'll need to specify it explicitly as per the documentation

Alternatively just shift your User model to \Entity instead of \Models and I think it'll be OK.