1

I'm just trying to inject the symfony serializer into an abstract class constructor, but that does not seem to work at all :

namespace App\DTO;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\SerializerInterface;

abstract class AbstractDTOMapper
{
    protected Serializer $serializer;

    public function __construct(SerializerInterface $serializer)
    {
        $this->serializer = $serializer;
    }

And that is the config in services.yaml:

  App\DTO\AbstractDTOMapper:
    abstract: true
    arguments:
      - '@serializer'

I still get this error :

Too few arguments to function App\DTO\AbstractDTOMapper::__construct(), 0 passed in C:\Users\me\Documents\project\src\Entity\Common\User.php on line 296 and exactly 1 expected
naspy971
  • 1,137
  • 2
  • 13
  • 31
  • As i see in the error message, you are trying to inject the `AbstractDTOMapper` inside your entity : `Entity\Common\User`. Entities are not services, and therefor, they are not aware of service container. Could you provide more context, how do you inject the DTOMapper in your `User` entity ? – Erwan Haquet May 12 '23 at 08:39
  • @ErwanHaquet AbstractDTOMapper is an abstract class so it's inherited by children DTO Mappers classes, and in this example, in User class, it's the child DTO Mapper that's being used. – naspy971 May 12 '23 at 08:42
  • I see. How are you instantiating the User DTO Mapper inside the User Entity ? Entities are not services and they are not aware of Symfony service container. It means that when you are doing : `User::__construct(UserDTOMapper $mapper)`, Symfony will not be able to resolve the `SerializerInterface` passed to the abstract. If i understand, you are trying to do `$entity->toDto()` ? I can clarify this in an answer if needed. – Erwan Haquet May 12 '23 at 09:25
  • @ErwanHaquet I do not inject the DTO Mapper in the user entity. I do it like this " return (new UserDTOMapper())->prepareJsonResponseData($this, $options);" and UserDTOMapper does not have a constructor. What i'm trying to do is to inject the serializer service in the AbstractDTOMapper because the serializer is used only in this abstract class – naspy971 May 12 '23 at 09:34
  • 1
    The parent inherit from its child constructor, in other words, you will have to pass the serializer to your DTO Mappers constructor `new UserDTOMapper($serializer)` (see https://stackoverflow.com/questions/14272598/what-is-the-use-of-constructor-in-abstract-class-in-php#answers-header). But, as much as you can, you'll want to avoid this situation and take profit of the Symfony D.I.. In your case, i encourage you to not tranform the entity to the DTO inside of your entity but to prefer something like `$dtoMapper->transform($entity)` (rather than `$entity->toDto()`). – Erwan Haquet May 12 '23 at 10:02
  • @naspy971 Just to put it another way, the PHP new operator knows nothing about the Symfony container. So `new UserDTOMapper` is a non-starter. You might consider making a [DTOServiceLocator](https://symfony.com/doc/current/service_container/service_subscribers_locators.html) to basically act as a DTO factory. – Cerad May 12 '23 at 12:06

0 Answers0