I am just starting to use Symfony and I just ran in to this problem and even after houers of research online I can not figure it out.
I am trying to insert data from a ajax request into my database. The ajax request works so far an send the following string
{"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 looks like this
/**
* @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 it i get the following error
Call to a member function get() on array
So the $params varible actually returns a object with all the data inside but I don't know how to set my Database variables with these values.