2016-08-04 115 views
4

我剛剛開始使用Symfony,而我剛剛遇到了這個問題,甚至在經過網上研究後,我無法弄清楚。Symfony 3 - 如何從Ajax響應數據庫中插入數據

我想從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實際上會返回一個包含所有數據的對象,但我不知道如何使用這些值設置我的數據庫變量。

+0

是什麼讓你覺得描述是問題?好像allDay沒有被張貼。 – Cerad

+0

,因爲all_day只是表格的第一列,這是第一個錯誤。因爲你可以使用request-> get('description')來獲得所有我試圖得到的值,它們都是null –

+0

好的。 json數據作爲內容發送。看到這裏:http://stackoverflow.com/questions/9522029/posting-json-objects-to-symfony-2此外,有一些組件(序列化等),可以使你的生活更容易。 – Cerad

回答

2

我想通了。如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); 
}