Symfony 4.4 Error on FOSUserBundle Installation

1.6k views Asked by At

I followed this two post about "how to install fosuserbundle in symfony 4.4":

https://vfac.fr/blog/how-install-fosuserbundle-with-symfony-4

https://ourcodeworld.com/articles/read/794/how-to-install-and-configure-fosuserbundle-in-symfony-4

But at the end i got this error:

Argument 3 passed to FOS\UserBundle\Doctrine\UserManager::__construct() must be an instance of Doctrine\Common\Persistence\ObjectManager, instance of Doctrine\ORM\EntityManager given, called in /url/to/symfony/proyect/var/cache/dev/ContainerKx7xY28/srcApp_KernelDevDebugContainer.php on line 1466

enter image description here i didn't change anything about FOSUserBundle but it's seems that something is wrong in my configuration...

My config files are these:

security.yaml

security:
    encoders:
        FOS\UserBundle\Model\UserInterface: bcrypt

    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: ROLE_ADMIN

    # https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
    providers:
        #users_in_memory: { memory: null }
        fos_userbundle:
            id: fos_user.user_provider.username

    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        main:
            #anonymous: lazy
            #provider: users_in_memory
            pattern: ^/
            form_login:
                provider: fos_userbundle
                csrf_token_generator: security.csrf.token_manager

            logout:       true
            anonymous:    true

    # Easy way to control access for large sections of your site
    # Note: Only the *first* access control that matches will be used
    access_control:
        - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/, role: ROLE_ADMIN }

packages/fos_user.yaml

# config/packages/fos_user.yaml
fos_user:
    db_driver: orm # other valid values are 'mongodb' and 'couchdb'
    firewall_name: main
    user_class: App\Entity\User
    from_email:
        address: "[email protected]"
        sender_name: "[email protected]"

src/Entity/User.php

<?php
// src/Entity/User.php

namespace App\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="fos_user")
 */
class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    public function __construct()
    {
        parent::__construct();
        // your own logic
    }
}

EDIT: I just tried the same guide with symfony 4.3 and it's work! so i think is something about compability of symfony 4.4 and FOSUserBundle...

2

There are 2 answers

3
Vincent PHILIPPE On BEST ANSWER

This is not a problem related to the fos user bundle, but with your declaration of user.

Read this post first.

See your User declaration :

<?php
// src/Entity/User.php

namespace App\Entity;

use Doctrine\Common\Persistence\ObjectManager; <---

Try replace it with

use Doctrine\ORM\EntityManagerInterface; <---

You may have somewhere in your code where you use an entity object which is declared with ObjectManager instead of EntityManagerInterface.

Please if this is not working explain why you add this line :

// AƱadimos esta linea porque parece que hacer algo...
use Doctrine\Common\Persistence\ObjectManager;

EDIT

Ok I've been looking for it, and that's look like a bug with doctrine. I found this issue : https://github.com/doctrine/orm/issues/8242.

It solve your problem.

Just update your composer.json like :

...
"require": {
        "php": ">=7.1.3",
        "ext-ctype": "*",
        "ext-iconv": "*",
        "composer/package-versions-deprecated": "^1.11",
        "doctrine/annotations": "^1.0",
        "doctrine/doctrine-bundle": "^2.1",
        "doctrine/doctrine-migrations-bundle": "^3.0",
        "doctrine/orm": "^2.7",
        "doctrine/common":"^2.13", <------
...

enter image description here

1
Juan Miguel On

I found a lot on internet finding the best solution and there was not one solved my problem, so I saw the services that uses the bundle and see that fos_user.user_manager.default: service is the one who call the Doctrine\Modle\UserManager so I rewrite it with my own class

fos_user.user_manager.default:
        class: App\Model\UserManager
        arguments:
            - '@fos_user.util.password_updater'
            - '@fos_user.util.canonical_fields_updater'
            - '@doctrine.orm.entity_manager'
            - '%fos_user.model.user.class%'
  1. Create your own class to manage your fosUser entity, it has to extends of FOS\UserBundle\Model\UserManager (can copy the code of the same class)
  2. Rewrite the service injecting the same parameters, except the third parameter, it will be replaced by EntityManagerInterface

I hope that will help you, It worked for me.