在EasyAdmin 3中使用预填充值重定向到NEW Action



我目前正在尝试添加克隆操作到我的EmployeeCrudController。
操作应该重定向到action::NEW视图,并有一些预填充的值。
但是我不知道如何预填这个表格。

下面是我在EmployeeCrudController中定义动作的地方:

public function configureActions(Actions $actions): Actions
{
$cloneAction = Action::new('Clone', '')
->setIcon('fas fa-clone')
->linkToCrudAction('cloneAction');
return $actions->add(Crud::PAGE_INDEX, $cloneAction);
}

这就是我的cloneAction的样子,它目前重定向到预期的Action::NEW,但没有预填充值:

public function cloneAction(AdminContext  $context): RedirectResponse
{
$id     = $context->getRequest()->query->get('entityId');
$entity = $this->getDoctrine()->getRepository(Employee::class)->find($id);
$clone = new Employee();
$entity->copyProperties($clone);
$clone->setFirstname('');
$clone->setLastname('');
$clone->setEmail('');
$routeBuilder = $this->get(CrudUrlGenerator::class);
$url = $routeBuilder->build([
'Employee_lastname' => 'test',
'Employee[teamMembershipts][]' => $clone->getTeamMemberships(),
])
->setController(EmployeeCrudController::class)
->setAction(Action::NEW)
->generateUrl()
;
return $this->redirect($url);
}

您可以使用选项data设置easyAdmin中字段的值。

$builder->add('Employee_lastname', null, ['data' => $clone->getTeamMemberships()]);

如果你的字段有多个选项,你可以使用choices和choices_value。

最新更新