Symfony – Ajax request update not working

I’m trying to update a Task with FullCalendar using the draggable and the droppable.
I’m doing an ajax request when we drop a task to update it. I sent the good task to my controller :

 eventDrop:function(info) {
                let start=moment(info.event.start).format('YYYY/MM/DD hh:mm');
                let end=moment(info.event.end).format('YYYY/MM/DD hh:mm');
                $.ajax({
                    url:"{{path('update_tache')}}",
                    data: 'start='+start+'&end='+end+'&id='+ info.event.id ,
                    type: "GET",
                    success: function(json) {
                        console.log(json);
                    },
                    error: function(request, error){
                        console.log(request);
                        alert(error);
                    }
                });
            },

I tried to update this task here :

/**
 * @Route("/tache/modifier_tache_from_agenda", name="update_tache")
 */
public function update_tache(Request $request, ObjectManager $manager)
{
    $tache = new Tache();
    $start = new DateTime($request->query->get('start'));
    $end = new DateTime($request->query->get('end'));

    $repository = $this
        ->getDoctrine()
        ->getManager()
        ->getRepository('App:Tache');

    $tache = $repository->find($request->query->get('id'));

    $tache->setDateDebut($start);
    $tache->setDateFin($end);

    $repository = $this
        ->getDoctrine()
        ->getManager()
        ->getRepository('App:EtatTache');
    $etatsTaches = $repository->findAll();

    $repository = $this
        ->getDoctrine()
        ->getManager()
        ->getRepository('App:Projet');
    $projets = $repository->findAll();

    $form = $this->createForm(TacheType::class, $tache, array(
        'etatsTaches' => $etatsTaches,
        'projets' => $projets
    ));

    if ($tache) {
        $form->handleRequest($request);

        $tache = $form->getData();
        $manager->persist($tache);
        $manager->flush();
    }

    return $this->json([
        'tache' => $tache
    ]);
}

I don’t see any error in the json returned (I moved the task from 9th december to 25th december) :

{
"tache": {
    "id": 5,
    "label": "en attente",
    "dateDebut": "2019-12-25T01:00:00+00:00",
    "dateFin": "2019-12-26T01:00:00+00:00",
    "description": "en attente",
    "projet": {
        "id": 1,
        "nom": "aaa",
        "description": "bbbb",
        "etat": {
            "id": 2,
            "etat": "actuel",
            "__initializer__": null,
            "__cloner__": null,
            "__isInitialized__": true
        },
        "dateDebut": "2020-01-03T00:00:00+00:00",
        "siteWeb": null,
        "__initializer__": null,
        "__cloner__": null,
        "__isInitialized__": true
    },
    "etat": {
        "id": 3,
        "etat": "En attente",
        "couleur": "#FFA500",
        "__initializer__": null,
        "__cloner__": null,
        "__isInitialized__": true
    }
}

}

But, the task aren’t updated in my database.

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *