0

我希望创建一个添加到新操作但基于添加的 url 参数预填充实体的操作。例如设置关联。

该文档显示了如何覆盖 createEntity 方法的设置值,但是此方法无法从请求中获取参数,因此我无法根据传递的参数设置关联。

这类似于如何在 AssociationField EasyAdmin 3 中设置默认值,但如本例所述,请求不可用。

4

1 回答 1

0

原来我们可以从请求堆栈中获取请求。

public function createEntity(string $entityFqcn)
{
    /** @var AgentAccreditation $entity */
    $entity = parent::createEntity($entityFqcn);

    $request = $this->get('request_stack')->getCurrentRequest();

    if ($agentId = $request->query->get('agentId')) {
        $agentRepository = $this->getDoctrine()->getRepository(Agent::class);
        $agent = $agentRepository->find($agentId);
        $entity->setAgent($agent);
    }

    return $entity;
}

}

于 2021-08-27T19:24:34.477 回答