FOSRestBundle - POST很好,但是当使用PUT / PATCH $请求为空时

时间:2014-10-28 14:53:02

标签: php symfony fosrestbundle

我正在关注本教程http://welcometothebundle.com/symfony2-rest-api-the-best-way-part-3/,然后我添加了一个新实体,作者。

使用GET,POST和DELETE一切都按预期进行,但是当使用PUT或PATCH时,我得到结果:

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

这是我收到的标题:

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

这是我的PHP代码

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();
    }
}

然后我插入了一个

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

在putAuthorAction中尝试阻塞之前,我得到一个空数组作为响应...

有没有人有类似的问题?

我已经尝试过这样做FosRestBundle post/put [create new/update entity] does not read Request correctly但没有成功..

我在Chromium中使用Postman扩展来测试...... (对不起我的英语,它不是我的主要语言)

0 个答案:

没有答案
相关问题