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

前端 未结 1 1225
谎友^
谎友^ 2021-02-09 04:06

Explanation:

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

  • indexAction(): lists all en
1条回答
  •  面向向阳花
    2021-02-09 04:56

    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_widget(deleteForms[entity.id]) }}

    0 讨论(0)
提交回复
热议问题