我有一个用 symfony 编程的约会网站。我想在 easyadmin 中添加一个功能,以便能够删除用户以及她/他的消息和帖子。在 UserCrudController 中,我按照 symfony 网站中的说明编写了一个自定义操作。但我收到一个错误
警告:array_merge():预期参数 2 为数组,给定 null
public function configureActions(Actions $actions): Actions
{
// this action executes the 'renderInvoice()' method of the current CRUD controller
$delUserAction = Action::new('deleteUser', 'Delete user completely')
->linkToRoute('delete_user', function (User $user){
$id = $user->getId();
$res = $this->getDoctrine()->getRepository(User::class)->find($id);
if ($res) {
$em = $this->getDoctrine()->getManager();
$em->remove($res);
$em->flush();
}
$res = $this->getDoctrine()->getRepository(Message::class)->remove_all_message($id);
$res = $this->getDoctrine()->getRepository(Beziehungen::class)->remove_all_relations($id);
$res = $this->getDoctrine()->getRepository(Album::class)->remove_album_of_user($id);
$res = $this->getDoctrine()->getRepository(Blog::class)->remove_blog_of_user($id);
$res = $this->getDoctrine()->getRepository(Comment::class)->remove_comments_of_user($id);
$res = $this->getDoctrine()->getRepository(Subcomment::class)->remove_subcomments_of_user($id);
$res = $this->getDoctrine()->getRepository(Like::class)->remove_likes_of_user($id);
});
return $actions
// ...
->add(Crud::PAGE_INDEX, $delUserAction)
;
}
你能帮我吗?
根据教程,
linkToRoute
会将动作连接到路线(顾名思义)。与其他路由函数类似,它期望第一个参数为路由名称,第二个参数为数组或返回数组的函数 - 用于填充路由占位符的参数。
您的函数不返回数组,而是返回操作。
所以你真的应该将你拥有的删除代码放入一个额外的函数中,例如......
deleteUserAction
,然后将你的操作定义为
$delUserAction = Action::new('deleteUser', 'Delete user completely')
->linkToCrudAction('deleteUserAction');