FosRestBundle post/put [create new/update entity] does not read Request correctly

久未见 提交于 2019-12-05 03:11:49
rollsappletree

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"

Try this :

if ($form->isValid()) {
    $entity = $form->getData();       //   <------ you didn't bind entity to the form
    $em = $this->getDoctrine()->getManager();
    $em->persist($entity);
    $em->flush();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!