5

I was following this tutorial http://welcometothebundle.com/symfony2-rest-api-the-best-way-part-3/ and then I've added a new entity, Author.

Using GET, POST and DELETE everything goes as expected, but when using PUT or PATCH I get as result:

[{"message":"An exception occurred while executing 'INSERT INTO Author 
(id, name, password) VALUES (?, ?, ?)' with params [16, null, null]:
\n\nSQLSTATE[23502]: Not null violation: (...)

This is the header I receive:

Allow →GET, PUT, PATCH, DELETE
Cache-Control →no-cache
Connection →keep-alive
Content-Type →application/json
Date →Tue, 28 Oct 2014 14:30:10 GMT
Server →nginx/1.1.19
Transfer-Encoding →chunked
X-Debug-Token →6d899c
X-Debug-Token-Link →/api/_profiler/6d899c
X-Powered-By →PHP/5.4.33-2+deb.sury.org~precise+1

This is my PHP code

public function putAuthorAction(Request $request, $id)
{
    try {
        if (!($author = $this->container->get('acme_blog.author.handler')->get($id))) {
            $statusCode = Codes::HTTP_CREATED;
            $author = $this->container->get('acme_blog.author.handler')->post(
                $request->request->all()
            );
        } else {
            $statusCode = Codes::HTTP_NO_CONTENT;
            $author = $this->container->get('acme_blog.author.handler')->put(
                $author,
                $request->request->all()
            );
        }

        $routeOptions = array(
            'id' => $author->getId(),
            '_format' => $request->get('_format'),
        );

        return $this->routeRedirectView('get_author', $routeOptions, $statusCode);

    } catch (InvalidFormException $exception) {
        return $exception->getForm();
    }
}

And then I inserted a

return $request->request->all();

before try block inside putAuthorAction and I get an empty array as response...

Does anyone had similar issue?

I already tried to do this FosRestBundle post/put [create new/update entity] does not read Request correctly but have no success..

I'm using Postman extension in Chromium to test ... (And sorry for my english, it's not my primary language)

Community
  • 1
  • 1
  • 7
    Well, finally I found out what is wrong... :( PUT and PATCH methods will receive the $request normally if the Content-Type is x-www-form-urlencoded this solved my problem... – Alex Keiti Nosse Oct 29 '14 at 22:55

0 Answers0