Symfony 2 EntityManager injection in service

Symfony 2 EntityManager injection in service

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

The Missing Constructor in Symfony Services

When building a custom service in Symfony, you expect dependency injection to work exactly as it does in standard PHP: the container instantiates your class and calls its __construct() method to wire up dependencies. If you are seeing a null property in your service despite having defined arguments in services.yml, the root cause is almost certainly a simple typo that breaks the entire initialization flow before Symfony even gets a chance to run your code.

In the scenario described, the developer defines an argument named entityManager in the YAML configuration file. However, the constructor method signature uses $entityManager. While these look similar, the immediate red flag in the provided code snippet is the spelling of the constructor itself: __constructor. In PHP, magic methods like constructors are case-sensitive and strictly defined by naming conventions. The correct spelling is __construct (lowercase 'c', no 'r').

Because the method name is misspelled as __constructor, PHP treats it as a regular public method rather than a special lifecycle hook. When Symfony's service container attempts to instantiate your UserService class, it bypasses this function entirely because it doesn't recognize it as the entry point for dependency injection. The container creates the object using new UserService(), leaving the protected $em property uninitialized (null). Once instantiated, the container then proceeds to call your custom method named __constructor. If you place a debug statement or an exit() call inside that method, it will indeed run, but by that time the critical wiring has already failed.

To resolve this, correct the spelling in your class file:

public function __construct(EntityManager $entityManager)
{
    // The logic below will now execute as expected
    $this->em = $entityManager;
}

Once corrected, ensure that your services.yml definition matches the constructor argument name exactly. In Symfony 2 and later versions, the container relies on the order of arguments in the constructor or the explicit names if type hinting isn't used for all parameters. Since EntityManager is a concrete class with a specific type hint, the container can map the YAML key entityManager to the parameter $entityManager.

There is also a secondary consideration regarding bundle isolation. The user noted that the Controller and the UserService reside in different bundles. This architecture is perfectly valid and does not inherently prevent dependency injection. As long as the service class is public (or marked as such) and registered in the application's main configuration via the import statement, the container can resolve dependencies across bundle boundaries. The fact that $this->get('doctrine.orm.entity_manager') works directly in the controller confirms that the Doctrine entity manager is correctly configured globally; the issue was strictly local to the UserService instantiation logic.

Another common pitfall involves how you access the service in a Controller within Symfony 2. If you are using a standard Controller class (not a bundle controller), ensure you are accessing services via the container or the appropriate injected property if using a specific controller type. However, calling $this->get() directly on a controller instance is generally discouraged because it couples your controller tightly to the service container implementation details. Instead, consider injecting the UserService directly into the controller's constructor or as a public property, allowing Symfony's autowiring (if available in your version) or explicit registration to handle the injection.

Finally, remember that debugging tools like Xdebug are invaluable here. Without them, seeing that a constructor wasn't called can be elusive. Setting a breakpoint at the start of __construct will immediately reveal if the method is being invoked. If the breakpoint is never hit, check your spelling first, then inspect the class visibility modifiers and ensure no traits or parent classes are interfering with the method resolution order.

Our network