vendredi 20 février 2015

Symfony 2 delete record from database on button click based on checkbox

I have messages listed on the page. Messages have their ids. I want to delete checked (checkbox) messages on button click.


Here is the controller action that deletes a single record:



/**
* @Route("/{_locale}/profile/inbox/delete", name="inbox_message_delete")
*/
public function deleteMessages(Request $request)
{
$em = $this->getDoctrine()->getManager();
$msg_id = $request->request->get('message-checkbox');
var_dump($msg_id);
$message = $em->getRepository('MyBundle:OfferMessages')->find($msg_id);

$em->remove($message);

$em->flush();

return new Response('O.K.');
}


My form looks like this:



<form action="{{path('inbox_message_delete')}}" method="post">


My submit button looks like this:



<input class="messages-received-delete-selected1" type="submit" value="{{'delete'}}"/>


Checkbox looks like this:



<input type="checkbox" name="message-checkbox[]" id="{{message.id}}" value="{{message.id}}" />


When I click the submit button I get the following page:



/profile/inbox/delete?message-checkbox=13492546


No var_dump() and new Response('O.K.') is shown.


How to refactor this code in order to work properly?





Aucun commentaire:

Enregistrer un commentaire