I want to update an entity into the db with an ajax call to the controller. However, I only know how to update entity using Symfony's form. Currently I have a form that will be appended by a jQuery method and submit it through ajax but am lost at what to do in the controller.
Ajax:
$("#editctrno").on("submit", function(event) {
event.preventDefault();
$.ajax({
url: "{{ path('containers_edit') }}",
type: "POST",
data: {'ctrno' : $("#ctrno").val(),
'refno' : $("#refno").val()},
dataType: "json",
success: function(data) {
console.log(data[0].ctrno);
}
});
});
Now on my controller:
/**
* @Route("/edit/", name="containers_edit", defaults={"_format" = "json"})
*/
public function editCtr(Request $request) {
$ctrno = $request->get('ctrno');
$refno = $request->get('refno');
$em = $this->getDoctrine()->getManager()->getRepository('Bundle:Ref');
// I want to access in the database to find the 'refno' and update the 'ctrno',
// something like:
// $entity = $em->findRefno($refno);
// $entity->setCtrno($ctrno);
// $em->flush();
return new Response(json_encode($entity));
}
Any suggestions for this?