gpt4 book ai didi

symfony - 如何在 EasyAdmin 3 中添加自定义操作?

转载 作者:行者123 更新时间:2023-12-03 23:04:52 25 4
gpt4 key购买 nike

我的实体 Participant 有一个 CrudController。我想添加一个自定义操作 sendAcknowledgementEmail。 EasyAdmin docs没有提及有关自定义函数参数或返回值的任何内容。
我有以下代码

public function configureActions(Actions $actions): Actions
{
$send_acknowledgement_email = Action::new('sendAcknowledgementEmail', 'Send Acknowledgement Email', 'fa fa-send')
->linkToCrudAction('sendAcknowledgementEmail');

return $actions
->add(Crud::PAGE_INDEX, $send_acknowledgement_email)
->add(Crud::PAGE_EDIT, $send_acknowledgement_email)
;
}

public function sendAcknowledgementEmail() //Do I need parameters?
{
//How do I get the Entity?

//What should I return?
}
到目前为止,EasyAdmin 检测到自定义函数,但我收到错误消息“ Controller 必须返回一个“Symfony\Component\HttpFoundation\Response”对象,但它返回了 null。您是否忘记在 Controller 中的某处添加 return 语句?
我如何从这里继续?

最佳答案

捆绑包的 v3.x 是相当新的,文档还不完善。
基于 Ceochronos 的回答,这是我对克隆操作的实现。

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

return $actions
->add(Crud::PAGE_INDEX, $cloneAction);

}

public function cloneAction(AdminContext $context)
{
$id = $context->getRequest()->query->get('entityId');
$entity = $this->getDoctrine()->getRepository(Product::class)->find($id);

$clone = clone $entity;

// custom logic
$clone->setEnabled(false);
// ...
$now = new DateTime();
$clone->setCreatedAt($now);
$clone->setUpdatedAt($now);

$this->persistEntity($this->get('doctrine')->getManagerForClass($context->getEntity()->getFqcn()), $clone);
$this->addFlash('success', 'Product duplicated');

return $this->redirect($this->get(CrudUrlGenerator::class)->build()->setAction(Action::INDEX)->generateUrl());
}

关于symfony - 如何在 EasyAdmin 3 中添加自定义操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63312446/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com