FosRestBundle发布/放置[创建新/更新实体]未正确读取请求 [英] FosRestBundle post/put [create new/update entity] does not read Request correctly

查看:93
本文介绍了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}

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

如果相反,我尝试使用此方法创建一个NEW实体:

/**
 * 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

所以我似乎无法正确传递绑定到表单的请求.

请注意,如果我按照建议json_decode的请求,请

有什么建议吗?

谢谢, 面包卷

解决方案

我解决了:)

这是它之前没有起作用的原因:

在我的表单定义中,名称为"zanzibar_backendbundle_countries".

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

因此,要将请求绑定到此表单,json应该看起来像这样:

{"zanzibar_backendbundle_countries": [{"description": "Japan"}]}

因为我希望它类似于

{"id":1,"description":"Italy"}

我必须从表单中删除该名称:

public function getName()
{
    return '';
}

通常,如果要发布带有

之类的占位符的json,

"something":{"key":"value"}

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

long story short: Using FOSRestBundle I'm trying to create some entities via POST call, or modify existing via PUT.

here the code:

/**
 * 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}

If I call /countries/{id} with PUT passing a json like {"description":"Japan"}, it modify my country with id=1, putting an empty description.

If, instead, I try to create a NEW entity with this method:

/**
 * 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

it gives me an error saying:

exception occurred while executing 'INSERT INTO countries (description) VALUES (?)' with params [null]:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'description' cannot be null

so seems that i'm not able to pass correctly the request to bind to the form.

Notice that if i json_decode the request as suggested here it reply with a

{
    "code":400,
    "message":"Validation Failed",
    "errors":{
        "errors":[
            "This value is not valid."
        ],
        "children":{
            "description":[
            ]
        }
    }
}

Any Advice?

Thanks, Rolls

解决方案

I solved :)

this is the reason why it did'nt worked before:

In my Form definition the name was "zanzibar_backendbundle_countries".

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

So to bind a request to this form the json should have looked like this:

{"zanzibar_backendbundle_countries": [{"description": "Japan"}]}

Since I wanted it to be something like

{"id":1,"description":"Italy"}

I had to remove the name from the Form:

public function getName()
{
    return '';
}

In general if you want to post a json with a placeholder like

"something":{"key":"value"}

your form name must be exactly "something"

这篇关于FosRestBundle发布/放置[创建新/更新实体]未正确读取请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆