Search code examples
ajaxsymfonysymfony-2.5jquery-3

set entity attribute (selecting a radio button) with ajax in Symfony 2.5


I want to integrate Ajax in my Symfony project (Symfony 2.5 and jQuery 3). I want to update an attribute of an Entity when I select a radio button. For now, I can get the id of the row that I select. I have searched how to implement this, but I have not succeeded.

JS code:

$(document).ready(function(){
    $("input:radio[name=locacion_destacada]").click(function () {
         var id = $(this).parent().parent().attr('dataId');
         alert(id);
         $.ajax({
             url: "/update-destacado",
             type: "POST",
             data: { id : id },
             success: function (response) {
                 alert(response);
             },
             error: function (XMLHttpRequest, textStatus, errorThrown) {
                 alert('Error: ' + errorThrown);
             }
         });

     });
});

Any help is greatly appreciated.


Solution

  • I could resolve it. I followed the idea of this page (answer n°8), also keep in mind your answers. Thanks for your help.

    Controller code:

    public function DestacadoAction(Request $request, $id){
    
        $em = $this->getDoctrine()->getManager();
    
        //Encontrar la locacion que ya estaba como destacada y dejarla como destacado=false
        $locacionDestacadaAntigua = $em 
            ->getRepository('FilmsBundle:Locaciones') 
            ->findOneBy(
                array(
                    'destacado' => true
                ));
    
        $locacionDestacadaAntigua->setDestacado(false);
        $em->persist($locacionDestacadaAntigua);
        $em->flush();
    
        $em = $this->getDoctrine()->getManager();
    
        //Dejar como destacada la nueva locacion
        $locacionDestacadaNueva = $this->getDoctrine() 
            ->getRepository('FilmsBundle:Locaciones') 
            ->findOneBy(
                array(
                    'idLocacion' => $id
                ));
        $locacionDestacadaNueva->setDestacado(true);
        $em->persist($locacionDestacadaNueva);
        $em->flush();
    
        return new Response("Ha seleccionado la locación \"" . $locacionDestacadaNueva->getNombreLocacion() . "\" como destacada.");
    }
    

    JS Code:

    $(document).ready(function(){
        $(".button").on("click", function (e) {
            $.post(this.href).done(function (response) {
                alert(response);
                location.reload();
            });
        });
    });
    

    Twig code:

    {% if locacion.destacado == true %}
        <td align="center">
            <a class="button" href="{{ path('admin_destacado_update', { 'id': locacion.idLocacion }) }}">
                <button class="btn btn-default">
                    <i class="glyphicon glyphicon-ok"></i>
                </button>
            </a>
        </td>
    {% else %}
        <td align="center">
            <a class="button" href="{{ path('admin_destacado_update', { 'id': locacion.idLocacion }) }}">
                <button class="btn btn-sm">
                    <i class="glyphicon glyphicon-remove"></i>
                </button>
            </a>
        </td>
    {% endif %}