symfony - FosRestBundle post/put [创建新/更新实体] 未正确读取请求

标签 symfony post request put fosrestbundle

长话短说:
使用 FOSRestBundle 我正在尝试通过 POST 调用创建一些实体,或通过 PUT 修改现有实体。

这里的代码:

/**
 * Put action
 * @var Request $request
 * @var integer $id Id of the entity
 * @return View|array
 */
public function putCountriesAction(Request $request, $id)
{
    $entity = $this->getEntity($id);
    $form = $this->createForm(new CountriesType(), $entity, array('method' => 'PUT'));
    $form->bind($request);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($entity);
        $em->flush();
        return $this->view(null, Codes::HTTP_NO_CONTENT);
    }

    return array(
        'form' => $form,
    );
} //[PUT] /countries/{id}

如果我调用/countries/{id} 并使用 PUT 传递像 {"description":"Japan"} 这样的 json,它会使用 id=1 修改我的国家/地区,并放置一个空描述。

相反,如果我尝试使用此方法创建一个新实体:
/**
 * Create new Countries (in batch)
 * @param  Request $request json request
 * @return array           redirect to get_coutry, will show the newly created entities
 */
public function postCountriesAction(Request $request)
{
    $entity = new Countries();
    $form = $this->createForm(new CountriesType(), $entity);
    $form->bind($request);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($entity);
        $em->flush();

        return $this->redirectView(
            $this->generateUrl(
                'get_country',
                array('id' => $entity->getId())
            ),
            Codes::HTTP_CREATED
        );
    }

    return array(
        'form' => $form,
    );
} //[PUT {"description":"a_description"}] /countries

它给了我一个错误说:
exception occurred while executing 'INSERT INTO countries (description) VALUES (?)' with params [null]:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'description' cannot be null

所以似乎我无法正确传递绑定(bind)到表单的请求。

请注意,如果我按照建议对请求进行 json_decode here它回复
{
    "code":400,
    "message":"Validation Failed",
    "errors":{
        "errors":[
            "This value is not valid."
        ],
        "children":{
            "description":[
            ]
        }
    }
}

有什么建议吗?

谢谢,
劳斯莱斯

最佳答案

我解决了:)

这就是它以前不起作用的原因:

在我的表单定义中,名称是“zanzibar_backendbundle_countries”。

public function getName()
{
    return 'zanzibar_backendbundle_countries';
}

因此,要将请求绑定(bind)到此表单,json 应该如下所示:
{"zanzibar_backendbundle_countries": [{"description": "Japan"}]}

因为我希望它像
{"id":1,"description":"Italy"}

我不得不从表格中删除名称:
public function getName()
{
    return '';
}

一般来说,如果你想发布一个带有占位符的 json
"something":{"key":"value"}

您的表单名称必须完全是“某物”

关于symfony - FosRestBundle post/put [创建新/更新实体] 未正确读取请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20567946/

相关文章:

perl - perl 生成的 POST 请求错误

symfony - 为什么 Composer 更新需要访问数据库

php - 无法使用默认身份验证登录

php - doctrine 是否支持多对多连接表中的复合键?

node.js - 将 HTTP 请求导出为模块函数 Node.js

Angular http post 请求内容类型从 'text/plain' 到 'application/json'

c# - HttpModule/HttpApplication 测试 url 是否为文件请求

postgresql - 使用 Doctrine2 和 PostgreSQL 的多数据库架构和迁移

php - 将此 .php 转换为 ObjC 可运行的 NSURLSession POST 请求?

php - 一种形式的两个下拉菜单