如何从具有实体列表(CRUD)的模板中删除实体? [英] How to delete an entity from a template with a list of entities (CRUD)?

查看:90
本文介绍了如何从具有实体列表(CRUD)的模板中删除实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已生成实体的CRUD,并获得以下默认操作:

I have generated the CRUD of an entity, getting the following default actions:


  • indexAction():列出所有实体。

  • showAction($ id):(按ID)查找并显示一个实体。

  • deleteAction($ id):删除一个实体。

  • 另一个动作。

  • indexAction(): lists all entities.
  • showAction($id): finds (by ID) and displays an entity.
  • deleteAction($id): deletes an entity.
  • another actions.

因此,我看到我只能删除使用参数$ id的动作中的实体(例如:showAction($ id)),但是我想删除indexAction模板中的实体,因为我将步骤保存到了用户。

So, I have seen I can only delete an entity within the actions that use the param $id (e.g.: showAction($id) ) but I want to delete an entity inside the indexAction template because I save a step to users.

deleteAction需要一个请求,一个ID并使用POST方法。

The deleteAction needs a request, an ID and use the POST method.

我正在尝试编写类似以下内容的代码:

I was trying to code something like:

<a href="{{ path('entity_delete', { 'id': entity.id }) }}" class="btn">
 <img src="{{ asset('bundles/acme/images/delete.png') }}" ... />
</a>

执行动作时,出现以下错误:

When I execute the action, I get the following error:


未找到 GET / acme / something / 4 / delete的路由:不允许使用方法
(允许发布,删除)

No route found for "GET /acme/something/4/delete": Method Not Allowed (Allow: POST, DELETE)

这个响应很清楚,这正是我所期望的,因此我尝试使用表格来做类似的事情。这样的事情:

This response is clear and it's what I expected, so I tried to do something similar but using a form. Something like this:

<form id="formDelete" action="{{ path('entity_delete', { 'id': entity.id }) }}" method="post">
    <input type="hidden" name="_method" value="DELETE" />
    {{ form_widget(delete_form) }}
    <a href="{{ url('entity_delete') }}" class="btn" onclick="document.getElementById('formDelete').submit();">
        <img src="{{ asset('bundles/acme/images/delete.png') }}" ... />
    </a>
</form>

但是行 {{form_widget(delete_form)}} 是一个问题,因为 indexAction()没有任何参数,并且需要以下代码:

But the line {{ form_widget(delete_form) }} is a problem because the indexAction() hasn't got any parameter and it needs this code:

$deleteForm = $this->createDeleteForm($id);
return $this->render('AcmeBundle:Demo:index.html.twig', array(
            'entities'      => $entities,
            'delete_form' => $deleteForm->createView(),
        ));

如您所见,对于方法 createDeleteForm,$ id参数是必需的($ id),但我无法从 indexAction()中获取它。

As you can see, the $id param is mandatory for the method createDeleteForm($id) but I can't get it from indexAction().

解决此问题的最佳方法是什么?

What is the best way to solve this issue?

推荐答案

如果您只想拥有与索引中的项目一样多的删除按钮,请按以下步骤操作。

if you only want to have as much delete buttons as items in your index here's how to easily do it.

在indexAction中,添加以下循环,不要忘记将参数传递给视图。

In the indexAction, add the following loop and don't forget to pass the parameter to the view.

public function indexAction()
{
    $em = $this->getDoctrine()->getManager();

    $entities = $em->getRepository('FooBundle:Link')->findAll();

    $deleteForms = array();

    foreach ($entities as $entity) {
        $deleteForms[$entity->getId()] = $this->createDeleteForm($entity->getId())->createView();
    }

    return array(
        'entities' => $entities,
        'deleteForms' => $deleteForms,
    );
}

Basicaly我只是遍历所有实体,并使用由crud生成的内置方法,将每个表单存储在数组中并传递给视图。

Basicaly I just loop over all my entities and create the corresponding delete form using the built-in method generated by the crud, storing each form in an array and passing it to the view.

然后在视图中,只需添加已在表单中提供的表单即可。 edit.html.twig生成的视图并编辑form_widget的参数:

Then in the view, just add the form already available in the edit.html.twig generated view and edit the form_widget's parameter:

<form action="{{ path('foo_delete', { 'id': entity.id }) }}" method="post">
    <input type="hidden" name="_method" value="DELETE" />
    {{ form_widget(deleteForms[entity.id]) }}
    <button type="submit" class="btn btn-small">
        <i class="icon-trash"></i>
        {{ 'links.admin.form.delete' | trans({}, 'FooBundle') }}
    </button>
</form>

这篇关于如何从具有实体列表(CRUD)的模板中删除实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆