-1

I have a file with employees and I want to import it and make a lot of validations.

I have a service which receives each loaded employee data, does a lot of validations, create or edit the employee based on his name, and persists the entity.

that do stuff with an employee and, after that, create or edit an employee entity and persists it.

class EmployeeModel()
{
    public function __contruct(string $employeeName);

    public function setEntityManager(EntityManager $em);
}

I don't want to use the container, I want to go with dependency injection.

If I make a

new EmployeeModel("John Doe");

I'll have no entityManager in the $this->em because the setter wasn't called.

I could make a factory, but I don't know how to pass a runtime variable like $employeeName.

All examples I found for parameters in factories work with defined parameters, like in parameters.yml.

model.employee:
    class:            AppBundle\EmployeeModel
    factory_service:  factory.employee
    factory_method:   get
    arguments:
        - "%this_exists_in_parameter_yml%"

So, what should be the best way to do this?

Basically I need to instantiate a class and, yet, have access to services but I don't want to use the container directly. I would like to go with dependency injection.

Btw, use forms validators won't work since I am not using forms.

Ali Akbarpour
  • 958
  • 2
  • 18
  • 35

1 Answers1

0

Never used it in an entity but can't you call EntityManager in the construct of your entity :

class EmployeeModel()
{
    private $em;

    public function __contruct(EntityManager $entityManager, string $employeeName) {
        $this->em = $em;
    }
}
Antoine Galluet
  • 320
  • 4
  • 14
  • It won't make different, since it's the same thing to use a setter and a constructor. In any case, I believe my logic is wrong. I need to split this between different layers – Eduardo Fernandes Mar 08 '18 at 16:55
  • I'm agree, as mentioned here for exemple https://stackoverflow.com/questions/14684745/get-entitymanager-inside-an-entity, you shouldn't have to use the EntityManager in you entity. – Antoine Galluet Mar 08 '18 at 16:58