Symfony 3 - How to insert data from Ajax Response to Database

I'm just starting to use Symfony, and I just ran into this problem, and even after researching online research, I can't figure it out.

I am trying to insert data from an ajax request into my database. The ajax request still works by sending the following line

{"description":"","location":"","subject":"asdfasdfdsf","allDay":false,"endTime":"2016-11-22T07:00:00.000Z","startTime":"2016-11-22T06:30:00.000Z","user":"6","calendar":"1","offer":"1","status":"open"}

Here is my ajax request

$.ajax({
                        type: 'POST',
                        url: '{{ path('calendar_new') }}',
                        contentType: 'application/json; charset=utf-8',
                        data: JSON.stringify(newAppointment),
                        dataType: 'json',
                        success: function(response) {
                            console.log(response);
                        }
                    });

My controller is as follows

/**
 * @Route("/calendar/new", name="calendar_new")
 * @Method({"GET", "POST"})
 */
public function calenderNewAction(Request $request)
{


    if ($request->isXMLHttpRequest()) {
        $content = $request->getContent();
        if (!empty($content)) {
            $params = json_decode($content, true);
            $new = new timeEntry;
            $new->setDescription($params->get('description'));
            $new->setLocation($params->get('location'));
            $new->setSubject($params->get('subject'));
            $new->setAllDay($params->get('allDay'));
            $new->setEndTime($params->get('endTime'));
            $new->setStartTime($params->get('startTime'));

            $em = $this->getDoctrine()->getManager();
            $calendar = $em->getRepository('AppBundle:calendar')
                ->findOneBy(['id' => 1]);

            $offers = $em->getRepository('AppBundle:offer')
                ->findOneBy(['id' => 1]);

            $new->setCalendar($calendar);
            $new->setOffer($offers);
            $new->setUser($this->getUser());

            $em->persist($new);
            $em->flush();
        }

        return new JsonResponse(array('data' => $params));
    }

    return new Response('Error!', 400);
}

After I try, I get the following error

Call to a member function get() on array 

Therefore, the $ params variable actually returns an object with all the data inside, but I don’t know how to set the database variables with these values.

+4
source share
1 answer

. Cerad, , .

.

 /**
 * @Route("/calendar/new", name="calendar_new")
 * @Method({"GET", "POST"})
 */
public function calenderNewAction(Request $request)
{


    if ($request->isXMLHttpRequest()) {
        $content = $request->getContent();
        if (!empty($content)) {
            $params = json_decode($content, true);
            $new = new timeEntry;
            $new->setDescription($params['description']);
            $new->setLocation($params['location']);
            $new->setSubject($params['subject']);
            $new->setAllDay($params['allDay']);
            $new->setEndTime(new \DateTime($params['endTime']));
            $new->setStartTime(new \DateTime($params['startTime']));

            $em = $this->getDoctrine()->getManager();
            $calendar = $em->getRepository('AppBundle:calendar')
                ->findOneBy(['id' => 1]);

            $offers = $em->getRepository('AppBundle:offer')
                ->findOneBy(['id' => 1]);

            $new->setCalendar($calendar);
            $new->setOffer($offers);
            $new->setStatus('Open');
            $new->setUser($this->getUser());

            $em->persist($new);
            $em->flush();
        }

        return new JsonResponse(array('data' => $params));
    }

    return new Response('Error!', 400);
}
+2

Source: https://habr.com/ru/post/1650157/


All Articles