微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – Symfony 3 – 如何将数据从Ajax响应插入数据库

我刚刚开始使用Symfony,我刚刚遇到这个问题,甚至在网上研究之后我也无法理解.

我试图将ajax请求中的数据插入到我的数据库中. ajax请求到目前为止一直发送以下字符串

{"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"}

这是我的ajax请求

$.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);
                        }
                    });

我的控制器看起来像这样

/**
 * @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);
}

我尝试后,我得到以下错误

Call to a member function get() on array 

所以$params varible实际上返回一个包含所有数据的对象,但我不知道如何使用这些值设置我的数据库变量.

解决方法:

我想到了.
正如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);
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐