4

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.

Daniel Schwarz
  • 121
  • 2
  • 9
  • What makes you think description is the problem? Seems like allDay is not being posted. – Cerad Aug 04 '16 at 12:07
  • because all_day is just the fist colume of the table and that is the first error. As you can se all values that i try to get with request->get('description') are null – Daniel Schwarz Aug 04 '16 at 12:42
  • Okay. json data is sent as content. See here: http://stackoverflow.com/questions/9522029/posting-json-objects-to-symfony-2 Also, there are a number of components (serializers etc) that can make life much easier for you. – Cerad Aug 04 '16 at 12:46
  • okay now i have all my data in a object but how can I set of my database object with these values? – Daniel Schwarz Aug 04 '16 at 13:25
  • I would think the same way you are currently doing it. Maybe update your question with the new code. – Cerad Aug 04 '16 at 13:30
  • I updated the question and as you can see now i allways get `Call to a member function get() on array` as an error – Daniel Schwarz Aug 04 '16 at 13:46
  • Given that $params is an array why would you try calling methods on it? Slow down a bit and try to understand what is going on. Maybe start with: http://php.net/manual/en/function.json-decode.php and http://symfony.com/doc/current/components/var_dumper.html – Cerad Aug 04 '16 at 13:50
  • Thanks for your help. I figured it out now. As you sad I called a method on it which makes sense that it didn't work. – Daniel Schwarz Aug 04 '16 at 14:30

1 Answers1

2

I figured it out. As mentioned by Cerad I called a method on an array which was the mistake.

Here is my now working controller.

 /**
 * @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);
}
Daniel Schwarz
  • 121
  • 2
  • 9