Reputation: 963
little trouble working something out. I have a /view-alerts page which displays my data. For each row of data I provide a delete option
<input type="button" value="Delete" data-url="{{ path('NickAlertBundle_delete') }}" onclick="delete_alert( {{ alert[0].id }} )"/>
So when this button is clicked, a javascript function is called.
function delete_alert(id){
var answer = confirm("Confirm delete");
if (answer){
$.ajax({
type: "POST",
url: $(this).attr('data-url'),
data: {row: id},
success: function(data) {
if(data){
var splitdata = data.split(":");
if(splitdata[0]=="Deleted"){
var id = splitdata[1];
alert("Your alert has been deleted");
}else{
alert(data);
}
}else{
alert("Unknown Error!");
}
},
error:function(){
alert("Please try again!");
}
});
}
So that calls my delete route, and should pass the id of the data row to be deleted. My route is currently like so
NickAlertBundle_delete:
pattern: /view-alerts
defaults: { _controller: NickAlertBundle:Alert:delete }
requirements:
_method: POST
I have the same url pattern as view-alerts because when they delete an alert from this page, I dont want them redirected to another page.
So now I am trying to access the id in my controller so I can delete the alert. At the moment I am trying
public function deleteAction(Request $request)
{
$id = $request->get('id');
$em = $this->getDoctrine()->getManager();
$alert = $em->getRepository('NickAlertBundle:AvailabilityAlert')->find($id);
if (!$alert) {
throw $this->createNotFoundException('Alert not found');
}
$alert->setIsDeleted(true);
$alert->setAlertStatus('Inactive');
$em->flush();
return new JsonResponse('Deleted');
}
The error I am currently receiving is
The identifier id is missing for a query of Nick\AlertBundle\Entity\AvailabilityAlert
How can I get the id to my controller with the way I am doing things (through an ajax request?)
Thanks
Upvotes: 2
Views: 3522
Reputation: 5067
Did you tried:
if($request->isXmlHttpRequest())
{
$id = (int)$request->request->get('row');
}
Instead of:
$id = $request->get('id');
?
Upvotes: 1