如何为构造器中具有变量的服务创建工厂



我有一个员工文件,我想导入它并进行大量验证。

我有一个服务,它接收每个加载的员工数据,进行大量验证,根据员工的名字创建或编辑员工,并保留实体。

与员工一起做事,然后创建或编辑员工实体并保留它。

class EmployeeModel()
{
public function __contruct(string $employeeName);
public function setEntityManager(EntityManager $em);
}

我不想使用容器,我想使用依赖注入。

如果我做一个

new EmployeeModel("John Doe");

我在 $this->em 中没有实体经理,因为没有调用二传手。

我可以创建一个工厂,但我不知道如何传递像 $employeeName 这样的运行时变量。

我在工厂中找到的所有参数示例都使用定义的参数,例如在 parameters.yml 中。

model.employee:
class:            AppBundleEmployeeModel
factory_service:  factory.employee
factory_method:   get
arguments:
- "%this_exists_in_parameter_yml%"

那么,最好的方法应该是什么?

基本上我需要实例化一个类,但是,可以访问服务,但我不想直接使用容器。我想使用依赖注入。

顺便说一句,使用表单验证器不起作用,因为我没有使用表单。

从未在实体中使用它,但你不能在实体的构造中调用 EntityManager:

class EmployeeModel()
{
private $em;
public function __contruct(EntityManager $entityManager, string $employeeName) {
$this->em = $em;
}
}

最新更新