Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete an entity from a template with a list of entities (CRUD)?

Explanation:

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

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

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.

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:

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>

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(),
        ));

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

Question:

What is the best way to solve this issue?

like image 510
luisete89 Avatar asked Aug 25 '13 19:08

luisete89


Video Answer


1 Answers

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

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 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.

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>
like image 179
DevAntoine Avatar answered Nov 15 '22 22:11

DevAntoine